找到匹配时剪掉一个字符串,然后继续查找匹配

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,...