Page 1 of 1

strtok()

Posted: Mon May 30, 2005 11:50 pm
by ibolui
hi, i cant get strtok() to work :oops: someone helps? thanks

Code: Select all

$str = &quote;a@b@c#d@e@f#g@h@i&quote;;
$temp = strtok($str,&quote;#&quote;);
while($temp) {
	echo &quote;temp = &quote;.$temp.&quote;<br>&quote;;
	$tok = $temp;
	$tok = strtok($tok,&quote;@&quote;);
	while($tok) {
		echo &quote;tok is &quote;.$tok.&quote;<br>&quote;;
		$tok = strtok(&quote;@&quote;);
	}
	$temp = strtok(&quote;#&quote;);
}
output is...
temp = a@b@c
tok is a
tok is b
tok is c

but i wanted a b c d e f g h i // each chars on a new line

Posted: Tue May 31, 2005 10:09 am
by pickle
I think strtok is getting confused because you're calling it on two different strings. Try explode()

Code: Select all

$each_line = explode($str,&quote;#&quote;);
foreach(entry in $each_line)
{
  $words_in_line = explode($each_line,'@');
  foreach(entry in $words_in_line)
  {
   echo word
  }
}

Posted: Tue May 31, 2005 11:58 am
by JAM
I guess this is to easy, but you didn't claim that the string would change, so... I can only answer what I can read.

Code: Select all

<pre>
<?php
$str = "a@b@c#d@e@f#g@h@i";
for ($i = 0; $i < strlen($str); $i += 2) {
    echo $str{$i}."\n";
}
?>

Posted: Tue May 31, 2005 12:23 pm
by ibolui
the string will change :)
but thanks!