从符号到符号查找文本的一部分

时间:2012-01-08 19:23:34

标签: php text symbols strstr

例如我有文字。

$text = 'Hello dear friend, my name is Jacky.
I am 25 years old and @I am earning* $500 per month, but @I am going* to earn more';

我想找到文本中从符号“@”开始并以符号“*”结尾的所有部分。 在这个例子中,我希望有这样的变量:

$found[1] = 'I am earning';
$found[2] = 'I am going';

谁可以帮我这个?我使用strstr(),但是当找到多个部件时它会失败。

2 个答案:

答案 0 :(得分:3)

preg_match_all('/@(.*?)\*/', $str, $matches);
$matches = $matches[1];

或者:

preg_match_all('/(?<=@).*?(?=\*)/', $text, $matches);
$matches = $matches[0];

两者都导致$matches等于:

Array
(
    [0] => I am earning
    [1] => I am going
)

答案 1 :(得分:1)

使用preg_match_all()

$text = 'Hello dear friend, my name is Jacky. I am 25 years old and @I am earning* $500 per month, but @I am going* to earn more';
preg_match_all("/(?<=@).*?(?=\*)/", $text, $found);

匹配项将存储在$found