找到匹配项时剪切字符串,然后继续查找匹配项


cut a string when find a match, and then continue finding matches

我正在尝试解决字符串问题。我需要搜索字符串,当它找到匹配项时,它应该回显匹配项并继续寻找更多匹配项。

我想我找到了解决方案的一部分:

    <?php
    $string = $view[ingredience];
    $showingre = strstr($string, '<p>');
    $show = strstr($showingre, ' ', true);
    echo $show;
    ?>

字符串正在搜索看起来像这样的数据($view[ingredience])

<p>100 tsk something</p><p>50 sptsk otherthing</p> ...

结果是它找到 100 并回显,但我需要一个循环,以便它可以找到 50 等等。

您可以使用

str_pos而不是strstr,以便传递偏移量。每次找到匹配项时,增加偏移量。

使用 while 循环或类似的东西继续搜索,直到达到 50(或字符串的末尾)。

你应该为此使用正则表达式

$regexPattern = "/<p>('d+)'s/gm";
$string = $view['ingredience'];
preg_match_all($regexPattern, $string, $matches);
//$matches will contains all your numbers 100,50,...