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 に答える