简体   繁体   English

如何使用正则表达式从字符串返回单词?

[英]How do I return words from strings using regular expressions?

I have some data which looks like: 我有一些看起来像的数据:

{i:0;s:2:"69";i:1;s:2:"70";i:2;s:2:"71";i:3;s:2:"72";i:4;s:2:"73";i:5;s:2:"74";i:6;s:2:"75";i:7;s:2:"76";i:8;s:2:"77";}

I need to remove all items within quotation marks, eg 69,70,..,77 . 我需要删除所有引号内的项目,例如69,70,..,77

I understand I should use the preg_split function. 我知道我应该使用preg_split函数。

I'm looking for help regarding the regular expression I need to pass to preg_split in order to return the required data. 我正在寻找有关正则表达式的帮助,该正则表达式需要传递给preg_split以便返回所需的数据。

Edit 编辑

Sorry, my initial question wasn't clear. 抱歉,我最初的问题不清楚。 I want to gather all values between speech marks and have them returned in an array (i'm not interested in keeping the rest of the data - all I require are the values within speech marks) 我想收集所有语音标记之间的值,并将它们返回到数组中(我对保留其余数据不感兴趣-我只需要语音标记内的值)

Edit 2 The data is a serialized object, I didn't include the full object in order to save space. 编辑2数据是一个序列化的对象,为了节省空间,我没有包括完整的对象。

without regexp: 没有正则表达式:

$v = 'a:9:{i:0;s:2:"69";i:1;s:2:"70";i:2;s:2:"71";i:3;s:2:"72";i:4;s:2:"73";i:5;s:2:"74";i:6;s:2:"75";i:7;s:2:"76";i:8;s:2:"77";}';
$v = unserialize($v);
print_r($v);
$v = array_combine(array_keys($v), array_fill(0, count($v), ''));
echo serialize($v); 

but this looks, how max mentioned, like a (part of a) serialized object. 但是看起来,max的提及方式就像序列化对象(的一部分)一样。
you should check if you are able to work with the complete syntax of it... 您应该检查您是否能够使用它的完整语法...

update 更新

well, if you only want the values of this serialized array, it gets even easier: 好吧,如果您只想要此序列化数组的值,它将变得更加容易:

$v = 'a:9:{i:0;s:2:"69";i:1;s:2:"70";i:2;s:2:"71";i:3;s:2:"72";i:4;s:2:"73";i:5;s:2:"74";i:6;s:2:"75";i:7;s:2:"76";i:8;s:2:"77";}';
$v = unserialize($v);
$v = array_values($v);
print_r($v);

output: 输出:

Array
(
    [0] => 69
    [1] => 70
    [2] => 71
    [3] => 72
    [4] => 73
    [5] => 74
    [6] => 75
    [7] => 76
    [8] => 77
)

array_values() isn't even necessary, really. 确实不需要array_values()

If you really want to delete all numbers between quotation marks, just use 如果您确实要删除引号之间的所有数字,请使用

$string = preg_replace('/"[0-9]+"/', '""', $string);

If you want to delete everything between these, use 如果要删除这些之间的所有内容,请使用

$string = preg_replace('/"[^"]+"/', '""', $string);

To your edited question: 对于您编辑的问题:

preg_match_all('/"([^"]+)"/', $string, $matches);

$matches will contain an array of arrays containing the strings. $ matches将包含一个包含字符串的数组数组。

Something like this should work: 这样的事情应该起作用:

$str = '{i:0;s:2:"69";i:1;s:2:"70";i:2;s:2:"71";i:3;s:2:"72";i:4;s:2:"73";i:5;s:2:"74";i:6;s:2:"75";i:7;s:2:"76";i:8;s:2:"77";}';
$str = preg_replace('#:"[^"]*"#', '', $str);
echo $str . "\n";

This will be more useful 这会更有用

$data = 'a:9:{i:0;s:2:"69";i:1;s:2:"70";i:2;s:2:"71";i:3;s:2:"72";i:4;s:2:"73";i:5;s:2:"74";i:6;s:2:"75";i:7;s:2:"76";i:8;s:2:"77";}';
$data = unserialize($data);
echo implode(',',$data);

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

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