我有一些像这样的字符串:
我:11; a:5:{s:2:“id”; s:4:“1097”; s:5:“iName”; s:12:“Ovo de Andre”; s:10: “dropChance”; S:4: “2000”; S:4: “类型”; i:1; s:5:“kName”; s:12:“Ovo de Andre”;
而且,我想知道我怎么能用这样的字符串代替字符串:“Ovo de Andre”:
“Ovo_de_Andre”,但它无法更改任何可能出现的空格。
我试过了:
$ string = preg_replace('/"(.?)\ s(。?)“/ m','”$ 1_ $ 2“',$ string);
但它只是替换了第一个空格,然后字符串看起来像这样:
我:11; a:5:{s:2:“id”; s:4:“1097”; s:5:“iName”; s:12:“Ovo_de Andre”; s:10:“ dropChance “; S:4:” 2000 “; S:4:” 类型“; i:1; s:5:“kName”; s:12:“Ovo_de Andre”;
我知道我可以用一段时间来检查它,但这对性能有问题,我认为这将是多余的。
此外,我想知道如何制作相同的内容,而不是替换“”中的空格,将其替换出来,而不是在“”中更改空格。
提前致谢。
我有这个链接,使类似的东西,但我无法管理什么改变,使其达到我想要的: Using preg_replace to replace all occurrences in php
这是完整的数据,我将其分成字符串,逐一检查是否存在错误。
$ string ='a:16:{'; $ string。='i:0; a:5:{s:2:“id”; s:4:“1113”; s:5:“iName”; s:5:“Drops”; s:10: “dropChance”; S:4: “7500”; S:4: “类型”; I:1; S:5: “kName”; S:5: “滴”;}'; $ string。='i:1; a:5:{s:2:“id”; s:4:“1585”; s:5:“iName”; s:11:“Mime Monkey”; s:10 :“dropChance”; s:4:“7000”; s:4:“type”; i:1; s:5:“kName”; s:11:“Mime Monkey”;}'; $ string。='i:2; a:5:{s:2:“id”; s:4:“1027”; s:5:“iName”; s:7:“Raptice”; s:10: “dropChance”; S:4: “7000”; S:4: “类型”; I:1; S:5: “kName”; S:7: “Raptice”;}'; $ string。='i:3; a:5:{s:2:“id”; s:4:“1002”; s:5:“iName”; s:6:“Poring”; s:10: “dropChance”; S:4: “7000”; S:4: “类型”; I:1; S:5: “kName”; S:6: “波令”;}'; $ string。='i:4; a:5:{s:2:“id”; s:4:“1767”; s:5:“iName”; s:8:“Deviling”; s:10: “dropChance”; S:4: “5000”; S:4: “类型”; I:2; S:5: “kName”; S:8: “Deviling”;}'; $ string。='i:5; a:5:{s:2:“id”; s:4:“1767”; s:5:“iName”; s:8:“Deviling”; s:10: “dropChance”; S:4: “5000”; S:4: “类型”; I:2; S:5: “kName”; S:8: “Deviling”;}'; $ string。='i:6; a:5:{s:2:“id”; s:4:“1766”; s:5:“iName”; s:8:“Angeling”; s:10: “dropChance”; S:4: “5000”; S:4: “类型”; I:2; S:5: “kName”; S:8: “Angeling”;}'; $ string。='i:7; a:5:{s:2:“id”; s:4:“1766”; s:5:“iName”; s:8:“Angeling”; s:10: “dropChance”; S:4: “5000”; S:4: “类型”; I:2; S:5: “kName”; S:8: “Angeling”;}'; $ string。='i:8; a:5:{s:2:“id”; s:4:“1004”; s:5:“iName”; s:6:“Zangão”; s:10: “dropChance”; S:4: “3500”; S:4: “类型”; I:1; S:5: “kName”; S:6: “Zangão”;}'; $ string。='i:9; a:5:{s:2:“id”; s:4:“1236”; s:5:“iName”; s:12:“Ovo de Andre”; s: 10:“dropChance”; s:4:“3000”; s:4:“type”; i:1; s:5:“kName”; s:12:“Ovo de Andre”;}'; $ string。='i:10; a:5:{s:2:“id”; s:4:“1076”; s:5:“iName”; s:9:“Esqueleto”; s:10: “dropChance”; S:4: “3000”; S:4: “类型”; I:1; S:5: “kName”; S:9: “Esqueleto”;}'; $ string。='i:11; a:5:{s:2:“id”; s:4:“1097”; s:5:“iName”; s:12:“Ovo de Andre”; s: 10:“dropChance”; s:4:“2000”; s:4:“type”; i:1; s:5:“kName”; s:12:“Ovo de Andre”;}'; $ string。='i:12; a:5:{s:2:“id”; s:4:“1051”; s:5:“iName”; s:14:“Besouro-Ladrão”; s: 10: “dropChance”; S:4: “2000”; S:4: “类型”; I:1; S:5: “kName”; S:14: “Besouro-Ladrão”;}'; $ string。='i:13; a:5:{s:2:“id”; s:4:“1183”; s:5:“iName”; s:16:“ChonChon Raivoso”; s:10 :“dropChance”; s:4:“1500”; s:4:“type”; i:1; s:5:“kName”; s:16:“ChonChon Raivoso”;}';
$ string。='i:14; a:5:{s:2:“id”; s:4:“1011”; s:5:“iName”; s:8:“ChonChon”; s:10: “dropChance”; S:4: “1500”; S:4: “类型”; I:1; S:5: “kName”; S:8: “飞头”;}'; $ string。='i:15; a:5:{s:2:“id”; s:4:“1784”; s:5:“iName”; s:5:“Stapo”; s:10: “dropChance”; S:4: “1000”; S:4: “类型”; I:1; S:5: “kName”; S:5: “Stapo”;}'; $ string。='}';
如果仔细观察,你会注意到i:0和i:11行中的一些数据之间的空格,显示了数据的行数和大小。
答案 0 :(得分:2)
这应该有效:
$str = <<< EOF
i: 11;a:5: {s:2:"id";s:4:"1097";s:5:"iName";s:12:"Ovo de Andre";s:10:"dropChance";s:4:"2000";s:4:"type"; i:1;s:5: "kName"; s:12: "Ovo de Andre"
EOF;
$str = preg_replace_callback('#("[^"]*")#',
create_function('$m',
'return str_replace(" ", "_", $m[1]);'),
$str);
echo $str . "\n";
更新:由于OP喜欢仅使用单一正则表达式进行此替换,因此这里使用负向前瞻:
$str = preg_replace('#\s(?!([^\"]*\"[^\"]*\")*[^\"]*$)#', '_', $str);
echo $str . "\n";
简而言之这个正则表达式找到一个 NOT 的空格,后跟偶数个双引号,这意味着找到的空格在双引号内,因此它被替换为 - 分数。
<强>输出:强>
i: 11;a:5: {s:2:"id";s:4:"1097";s:5:"iName";s:12:"Ovo_de_Andre";s:10:"dropChance";s:4:"2000";s:4:"type"; i:1;s:5: "kName"; s:12: "Ovo_de_Andre"
答案 1 :(得分:1)
编辑:OP澄清该字符串不是有效的序列化对象......
我不确定你是否会进一步操纵这个对象,或者它来自哪里,但是如果它是我的话我会做的第一件事是将它反序列化(这假设你有一个序列化的对象:它看起来确实如此,虽然您发布的确切字符串不是有效的序列化对象。我假设您没有发布整个字符串。)
http://www.php.net/manual/en/function.unserialize.php
一旦您的阵列采用合理的格式,您就可以开始在每个属性的基础上应用转换......
//unserialize
$person = unserialize($yourstring);
// now perfrom transformations
$person['iName'] = str_replace(' ', '_', $person['iName']);
$person['kName'] = str_replace(' ', '_', $person['kName']);
// etc...
// now re-serialize
$person = serialize($person);
使用正则表达式对对象/数组的属性进行大量转换只是一场噩梦 - 让自己轻松自如,并使用php本机功能来帮助你!
如果不有序列化对象,请忽略此答案。
答案 2 :(得分:0)
如果可能,我会避免使用正则表达式,因为您可以轻松地反序列化并读取值,然后将其序列化。这将确保您确切知道您正在改变的内容。
http://www.php.net/manual/en/function.unserialize.php
function space_to_underscore($str) {
return str_replace(' ', '_', $str);
}