大家好,我需要一些帮助,我是PHP中的新手。我有这样的字符串
$string="+note1-note2-note3+note4-note5-note6+note7-note8-note10";
我只需要提取具有以下+
的部分来数组:note1
、note4
、note7
。
有人可以帮助我吗?非常感谢!!
您可以使用正则表达式轻松完成此操作。
$string="+note1-note2-note3+note4-note5-note6+note7-note8-note10";
preg_match_all('/'+(note'd+)/', $string, $matches);
print_r($matches[1]);
输出:
Array
(
[0] => note1
[1] => note4
[2] => note7
)
正则表达式101演示:https://regex101.com/r/pW3eS0/1
'd
是一个数字,第二个+
是一个量词,表示前面的字符/组的一个或多个。第一个加号'+
是文字的,前面的'
使其成为实际的+
字符,否则会导致错误,因为它将是量词,但什么都没有量化。
PHP 演示:https://eval.in/527661