0

例如我有文字。

$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(),但是当找到多个部分时它会失败。

4

2 回答 2

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
)
于 2012-01-08T19:25:54.973 回答
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.

于 2012-01-08T19:29:26.380 回答