简体   繁体   English

替换“”中的空格,preg_replace php

[英]Replace Spaces within “”, preg_replace php

I have some strings like this: 我有一些像这样的字符串:

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: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"; i:1; s:5:“kName”; s:12: "Ovo de Andre"; s:12:“Ovo de Andre”;

And, I'd like to know how I could replace that string: "Ovo de Andre" to something like this: 而且,我想知道我怎么能用这样的字符串代替字符串:“Ovo de Andre”:

"Ovo_de_Andre", but it can't change any spaces that could be out "". “Ovo_de_Andre”,但它不能改变任何可能出“”的空格。

I tried: 我试过了:

$string = preg_replace('/"(. ?)\\s(. ?)"/m', '"$1_$2"', $string); $ string = preg_replace('/“(。 ?)\\ s(。 ?)”/ m','“$ 1_ $ 2”',$ string);

But it just replaces the first space, then the string looks like this: 但它只是替换了第一个空格,然后字符串如下所示:

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: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"; i:1; s:5:“kName”; s:12: "Ovo_de Andre"; s:12:“Ovo_de Andre”;

I know I could use a while to check it, but it'd be problematic to performance and I think it'd would be redundant. 我知道我可以用一段时间来检查它,但它对性能有问题,我认为这将是多余的。

Additionally, I want to know how could it be to make the same, but rather than replacing spaces within "", replace those out, without change spaces within "". 另外,我想知道如何制作相同的内容,而不是替换“”中的空格,将其替换掉,而不是在“”中更改空格。

Thanks in advance. 提前致谢。


I have this link that makes something similar but I couldn't manage what changing to make it reach what i want: Using preg_replace to replace all occurrences in php . 我有这个链接,使类似的东西,但我无法管理什么改变,使其达到我想要的: 使用preg_replace替换PHP中的所有出现

Here is the full data, I separeted it in strings to test one by one looking for errors that were there. 这是完整的数据,我将其分成字符串,逐一检查是否存在错误。

$string = 'a:16:{'; $ 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:"type";i:1;s:5:"kName";s:5:"Drops";}'; $ 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: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:"type";i:1;s:5:"kName";s:7:"Raptice";}'; $ 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:"type";i:1;s:5:"kName";s:6:"Poring";}'; $ 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:"type";i:2;s:5:"kName";s:8:"Deviling";}'; $ 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:"type";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:"type";i:2;s:5:"kName";s:8:"Angeling";}'; $ 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:"type";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:"type";i:1;s:5:"kName";s:6:"Zangão";}'; $ 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: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:"type";i:1;s:5:"kName";s:9:"Esqueleto";}'; $ 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: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:"type";i:1;s:5:"kName";s:14:"Besouro-Ladrão";}'; $ 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: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:"type";i:1;s:5:"kName";s:8:"ChonChon";}'; $ 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:"type";i:1;s:5:"kName";s:5:"Stapo";}'; $ 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 .= '}'; $ string。='}';

If looked carefully you'll notice in the lines i:0 and i:11 of the vector some spaces between the data that shows what line number and size of data exists. 如果仔细观察,你会注意到i:0和i:11行中的一些数据之间的空格,显示了数据的行数和大小。

This should work: 这应该工作:

$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";

Update: Since OP likes to have this replacement with single regex only, here is one using negative lookahead: 更新:由于OP喜欢仅使用单个正则表达式进行替换,因此这里使用负向前瞻:

$str = preg_replace('#\s(?!([^\"]*\"[^\"]*\")*[^\"]*$)#', '_', $str);
echo $str . "\n";

In short this regex finds a space which is NOT followed by even number of double quotes, which means found space is within double quotes hence it is replaced by under-score. 总之此正则表达式发现其后面没有偶数双引号的,这意味着发现空间的空间是双引号因此它是由下分数替换内。

OUTPUT: OUTPUT:

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"

Edit: OP has clarified that the string is not a valid serialized object... 编辑:OP已澄清该字符串不是有效的序列化对象...

I'm not sure if you will be manipulating this object further, or where it came from, but the first thing I would do if it was me is unserialize it (this is making the assumption that you have a serialised object there: it certainly looks like it, although the exact string you posted is not a valid serialized object. I'm making the assumption that you didn't post the whole string). 我不确定你是否会进一步操纵这个对象,或者它来自哪里,但是如果它是我的话我会做的第一件事是将它反序列化(这假设你在那里有一个序列化的对象:它当然看起来像它,虽然您发布的确切字符串不是有效的序列化对象。我假设您没有发布整个字符串)。

http://www.php.net/manual/en/function.unserialize.php http://www.php.net/manual/en/function.unserialize.php

Once you have your array in a sensible format you can start applying your transformations on a per attribute basis... 一旦你的阵列以合理的格式存在,你就可以开始在每个属性的基础上应用你的变换......

//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);

Performing masses of transformation on attributes of an object/array using regex is just a nightmare - make it easy on yourself and use php native functionality to help you! 使用正则表达式对对象/数组的属性执行大量转换只是一个噩梦 - 让自己轻松自如,并使用PHP本机功能来帮助您!

If you dont have a serialised object then disregard this answer. 如果你没有序列化的对象,那么忽略这个答案。

I would avoid using regex if possible, because you can easily unserialize and read the value, then serialize it back. 如果可能的话,我会避免使用正则表达式,因为您可以轻松地反序列化并读取值,然后将其序列化。 This will ensure that you know exactly what you're changing. 这将确保您确切地知道您正在改变什么。

http://www.php.net/manual/en/function.unserialize.php http://www.php.net/manual/en/function.unserialize.php

function space_to_underscore($str) {
    return str_replace(' ', '_', $str);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM