...

View Full Version : Adding a while loop ?



jeddi
11-04-2009, 02:45 PM
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.



while( ???) {
$pos1 = strpos($text, $findme1, $off_set);
if ($pos1 === false) {
echo "<br>$findme1 not found";
exit();
}
$text = substr($text,$pos1);
preg_match("/href=\"(.*?)\"/",$text,$matches, PREG_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

Fumigator
11-04-2009, 04:09 PM
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-structures.break.php

jeddi
11-04-2009, 05:42 PM
So I guess that I should just use the break to end the loop like this ?


while($pos1 > 0 ) {
$pos1 = strpos($text, $findme1, $off_set);
if ($pos1 === false) {
echo "<br>$findme1 not found";
break;
}
$text = substr($text,$pos1);
preg_match("/href=\"(.*?)\"/",$text,$matches, PREG_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 ?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum