Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

NEWB loop through text

Status
Not open for further replies.

salewit

Programmer
Oct 31, 2002
58
US
I know this is probably very simple, but I can't figure it out. I've tried it with a WHILE and a DO loop, and can't get it to work.

$string = "This is a test. This is only a test. Testing 123";
$position = strpos($string,"test");
if ($position !== false) {
// processes
// more processing
}

How do I keep this loop going until I've gone through the string? I know this is incredibly simple, but I'm stuck.
 
Code:
$string = "This is a test. This is only a test. Testing 123";
$position = 0
while ($position = strpos($string,"test", $position) !== FALSE):
// processes
// more processing
endwhile;
 
oops - don't forget the semicolon after the
Code:
$position = 0; //semicolon
 
Thanks for the response. I tried this and it resulted in an endless loop.

$position = 0
while ($position = strpos($string,"test", $position) !== FALSE):

Doesn't using the variable $position twice in this statement throw off the loop? Maybe it should be something like:

$start_position = 0
while ($position = strpos($string,"test", $start_position) !== FALSE):
$start_position = $position;

I don't know... I haven't tried it yet.

Also, this threw me off on the bracketing. I've never seen a loop started with a colon and ending with a semi-colon and not the curly brackets. I didn't get an error, so it must be right. Just never saw that.

Sam

PS Hmmm I just tried my alteration, and it didn't work either. Endless loop. Stuck again.
 
Also, this threw me off on the bracketing. I've never seen a loop started with a colon and ending with a semi-colon and not the curly brackets. I didn't get an error, so it must be right. Just never saw that.
It's an alternate syntax. I recommend strongly against its use because, as you can see, it can cause readability issues. But it's in the manual:

Are you looking for a loop that runs through a string a character at a time? How 'bout:

Code:
<?php
$the_string = 'This is a test. This is only a test. Testing 123';

for ($counter = 0; $counter < strlen($the_string); $counter++)
{
   print $the_string{$counter};
   print "\r\n";
}
?>



Want the best answers? Ask the best questions! TANSTAAFL!
 
Actually, I found a routine that does what I want. I don't know HOW it does it, but it works.

What I was trying to do was to look for instances of any kind of link ( in my text string and add the tags to make it clickable. It got ridiculously complex and the routine I found used eregi_replace which basically did it all on one line. It's gonna be quite awhile before I can begin to understand how that one works.

Thanks for the help.
Sam
 
NEWB

sorry - basic mistake forgetting to increment the position counter. here is a revised script (with some built in loop prevention).

Sleipnir214 is right that the colon is part of the alternative syntax. quite opposite to sleipnir, however, i have been using it for the last while because i believe it enormously enhances readability and reduces the chance of syntax errors (particularly for nested loops and conditionals).

Code:
$string = "This is a test. This is only a test. Testing 123";
$position = -1;
$cnt = 0;
$len = strlen($string);
$increment = 1; // this will be ok but you may find it better to increment by the length of the needle
$needle = "test";
while (($position = strpos($string, $needle, $position + $increment)) !== FALSE):
// processes
// more processing
	echo "position is $position <br/>";
	if ($cnt > $len ):
		break;
	else:
		$cnt ++;
	endif;
endwhile;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top