Anyone know what to do about multiple occurances with strpos? I need to get all of the occurances from a string.

Basically I have a HTML file and I want to grab all the links from it.

    do it in a loop...

    $occurances = array();
    $start = 0;
    while($start = strpos($heystack, $find_me, $start)
    {
    $occurances[] = $start;
    }

    this will put all occurances in an array called $occurances.. neet or what?

    Andreas

      • [deleted]

      shouldn't you do a

      $start++;

      after the $occurances.. line?

      otherwise it will continue to look at the position where it just found an occurance...

        No... I got a java function that does almost the same thing (using String.indexOf().. it works fine there... but you might be right... I havent tried it...

        Andreas

          • [deleted]

          Actually it's "YES" :-)

          The code as posted will loop forever if any match is found. You must increase $start by one after ever match of it will start it's next search at the position of the last match, which will be a match immediately, at the same location, which will start searching at the same location which gives a match etc etc...

            Write a Reply...