Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Adding a while loop ?

    Hello,

    I have the following code to find the $findme1 in the
    text string.

    What I want to do is to continue finding the finds until the end of the
    string. So I have wrapped it with a while, but I am not sure what expression to use to make it continue until the end of the file

    Now, before you write that I should use preg_match_all(), there is a reason for me wanting to skip a lot of the matches which is why I am locating the $findme1.


    PHP Code:
    while(  ???) {
     
    $pos1 strpos($text$findme1$off_set);
        if (
    $pos1 === false) {
        echo 
    "<br>$findme1 not found";
            exit();
        }
        
    $text substr($text,$pos1);
        
    preg_match("/href=\"(.*?)\"/",$text,$matchesPREG_OFFSET_CAPTURE);

      
    print_r($matches);
      
    $saved_url $matches[1][0];
      
    $off_set $matches[1][1];
      echo 
    "<br><br><br>Match: $saved_url<br><br>";

      
    $urls[] = $saved_url;


    Oh - and if I use break: instaed of exit:, will the script continue on after the while loop ?

    Thanks

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Normally you would check $pos1 to see if it's greater than the length of the string $text, but then you are re-assigning the value of $text inside your loop so you'll probably have problems because of it.

    And yes, the break command breaks out of the current code block {}.

    http://us2.php.net/manual/en/control...ures.break.php

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,674
    Thanks
    28
    Thanked 4 Times in 4 Posts
    So I guess that I should just use the break to end the loop like this ?

    PHP Code:
    while($pos1 ) {
     
    $pos1 strpos($text$findme1$off_set);
        if (
    $pos1 === false) {
        echo 
    "<br>$findme1 not found";
            break;
        }
        
    $text substr($text,$pos1);
        
    preg_match("/href=\"(.*?)\"/",$text,$matchesPREG_OFFSET_CAPTURE);

      
    print_r($matches);
      
    $saved_url $matches[1][0];
      
    $off_set $matches[1][1];
      echo 
    "<br><br><br>Match: $saved_url<br><br>";

      
    $urls[] = $saved_url;


    I suppose that is valid ?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •