PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
ibolui
Forum Commoner
Posts: 27 Joined: Thu May 26, 2005 9:41 am
Post
by ibolui » Mon May 30, 2005 11:50 pm
hi, i cant get strtok() to work
someone helps? thanks
Code: Select all
$str = "e;a@b@c#d@e@f#g@h@i"e;;
$temp = strtok($str,"e;#"e;);
while($temp) {
echo "e;temp = "e;.$temp."e;<br>"e;;
$tok = $temp;
$tok = strtok($tok,"e;@"e;);
while($tok) {
echo "e;tok is "e;.$tok."e;<br>"e;;
$tok = strtok("e;@"e;);
}
$temp = strtok("e;#"e;);
}
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
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue May 31, 2005 10:09 am
I think strtok is getting confused because you're calling it on two different strings. Try explode()
Code: Select all
$each_line = explode($str,"e;#"e;);
foreach(entry in $each_line)
{
$words_in_line = explode($each_line,'@');
foreach(entry in $words_in_line)
{
echo word
}
}
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
JAM
DevNet Resident
Posts: 2101 Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:
Post
by JAM » Tue May 31, 2005 11:58 am
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";
}
?>
ibolui
Forum Commoner
Posts: 27 Joined: Thu May 26, 2005 9:41 am
Post
by ibolui » Tue May 31, 2005 12:23 pm
the string will change
but thanks!