$str = (( test: 'object1', test2: 'object2', test3: 'object3' ))
$ str =((test:'object1',test2:'object2',test3:'object3'))
Out of the above string, I want to extract the word object1
.
在上面的字符串中,我想提取单词object1。
So, I wrote following code, but it doesn't work.
所以,我写了下面的代码,但它不起作用。
<?php
echo preg_replace("/^(.+)test:(.+)'(.+)'(.+)$/", "$3", $str);
?>
It printed "object3"
它打印“object3”
How can I fix this problem?
我该如何解决这个问题?
1 个解决方案
#1
1
You can simply use this regex:
你可以简单地使用这个正则表达式:
.*?test:\s*'(.+?)'.*
Your example:
你的例子:
$str = "(( test: 'object1', test2: 'object2', test3: 'object3' ))";
echo preg_replace("/.*?test:\s*'(.+?)'.*/", "$1", $str); //: object1
#1
1
You can simply use this regex:
你可以简单地使用这个正则表达式:
.*?test:\s*'(.+?)'.*
Your example:
你的例子:
$str = "(( test: 'object1', test2: 'object2', test3: 'object3' ))";
echo preg_replace("/.*?test:\s*'(.+?)'.*/", "$1", $str); //: object1