只有符号拆分后PHP字符串 [英] Split php string only after symbol
问题描述
大家好我需要一些帮助,
即时通讯在PHP新。我有这样一个字符串
Hi everybody i need some help, im new in php. I have a string like this
$string="+note1-note2-note3+note4-note5-note6+note7-note8-note10";
我需要提取到阵列只与 +
喜欢的部分:注1
,注4
,注7
。
有人能帮助我吗?
非常感谢!
Can someone help me? Thanks so much!!
推荐答案
您可以轻松地用正则表达式做pretty。
You can do this pretty easily with a regex.
$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
)
Regex101演示: https://regex101.com/r/pW3eS0/1
\\ D
是一个数字,第二个 +
是一个量词意思是1或以上的$ P $的pceding字符/组。第一个加 \\ +
是文字,则preceding \\
使得它的实际 +
字符,否则会引起错误,因为这将是量词,但要量化罢了。
\d
is a number and the second +
is a quantifier meaning 1 or more of the preceding character/group. The first plus \+
is literal, the preceding \
makes it the actual +
character, otherwise it would cause an error because it would be the quantifier but be quantifying nothing.
PHP演示: https://eval.in/527661
这篇关于只有符号拆分后PHP字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!