Search code examples
phpstringwhile-loopwiki

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


I am trying to solve a problem with strings. I need to search the string and when it finds a match it should echo the match and continue looking for more matches.

I think I found a part of the solution:

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

The string is searching data that are looks something like this ($view[ingredience])

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

and the result is that it finds 100 and echo, but I need a loop so it can find 50 and so on.


Solution

  • You can use str_pos instead of strstr so you can pass in an offset. Every time you find a match, increase the offset.

    Use a while-loop or something similar to keep searching untill you reach 50 (or the end of the string).