Jump to content

Recommended Posts

 

 

Folks,

 

I found this code in the manual:

 


<?php
 
$string = "This is\tan example\nstring";
/* Use tab and newline as tokenizing characters as well  */
$tok = strtok($string, " \n\t");
 
while ($tok !== false) {
    echo "Word=$tok<br />";
    $tok = strtok(" \n\t");
}
 
?>

 

It outputs result:

 

string(9) "something" bool(false)

 

 

Now, I have questions.

 

Q1. How does php know that both:

"\n" & "\t" are not the delimiter. But either 1 ?

I mean, if it was like this then I can assume how php would figure either one is the delimiter:

 

$tok = strtok($string, " \n, \t");

 

Note, the double quotes represent the 2nd parameter. And, the comma inside the 2nd parameter represents ht OR.

Alternatively:

 

$tok = strtok($string, " \n\\t");

$tok = strtok($string, " \n OR \t");

 

 

Q2. Let us say, I do not want either the following to be the delimiter:

 

\n

\t

 

But I want both. Like any of these:

 

\n \t

\n\t

 

$tok = strtok($string, " \n \t");

$tok = strtok($string, " \n\t");

 

Then how would I code it ? Because if I put both of them together (with no comma separating them from within the 2nd parameter) then php would assume I am trying to get it to take either 1 as the delimiter when either is found.

 

Good questions. Yes ? :)

 

 

 

1. Because they used the right magic pixie dust when they made the function. Which is great because if they had they used the wrong magic pixie dust then calling the function might have imploded your computer.

2. Don't use strtok.

3. Not really, no.

From the manual:

splits a string into smaller strings, with each token being delimited by any character from [token].

(emphasis mine)

 

If you want two characters as the token, then use something else. Probably regular expressions, since you're talking about there maybe or maybe not being a space.

Edited by Sepodati
  • Like 1

In general, just forget strtok even exists. Use explode or preg_split if you want to split a string up by a delimiter. Use explode if your delimiter is a fixed value (ie a comma, new line, etc) and preg_split if your delimiter is a bit more complex (ie, any form of white-space).

 

strtok is a carry-over from C, so it functions the same way the C version does. As for why it treats the string as a set of single-character delimiters, I guess that's what was needed when the original C function was defined and PHP just carried that forward. There's no way to change this behavior, if you want to use a multi-character delimiter, you cannot use strtok.

  • Like 1

From the manual:

 

(emphasis mine)

 

If you want two characters as the token, then use something else. Probably regular expressions, since you're talking about there maybe or maybe not being a space.

 

In other words, the token must be a single character with no spaces. And if I want more than a single character like a double character, WITH or WITHOUT space then I must use regex. Correct ?

If so, then how-about 2 examples from your end ? I am sure this would be a good extra learning for all newbies, thanks to you! ;)

But if it's going to be trouble for you, then "oh well", then no worries.

 

Once again, thanks! :)

Edited by phpsane

In general, just forget strtok even exists. Use explode or preg_split if you want to split a string up by a delimiter. Use explode if your delimiter is a fixed value (ie a comma, new line, etc) and preg_split if your delimiter is a bit more complex (ie, any form of white-space).

 

strtok is a carry-over from C, so it functions the same way the C version does. As for why it treats the string as a set of single-character delimiters, I guess that's what was needed when the original C function was defined and PHP just carried that forward. There's no way to change this behavior, if you want to use a multi-character delimiter, you cannot use strtok.

 

I LIKED your answer. Would pick it as BEST ANSWER and close this thread. But let's wait  and see if Sedopati bothers to answer my last question to him.

Alternatively, you are free to answer it and I can close this thread as RESOLVED by Kicken.

So, C did things this way, hey ? I am not into C. But thanks for bringing this to my attention. We know why php syntax is messy. Copied most of it from C. Would have been better if they did not do this and came-up with their own clean syntax.

 

 

Btw, you said:

"Use explode if your delimiter is a fixed value (ie a comma, new line, etc) and preg_split if your delimiter is a bit more complex (ie, any form of white-space)."

 

We newbies, are likely to forget this. But if we see code examples then that gets imprinted into our minds and we tend to remember things. And so, if it is not too much trouble then 2 examples for the "explode" and "preg_split" would be great.
Yes, I know the manual shows examples but it won't show examples based on our discussion. Now, will it ? ;)
Edited by phpsane

1. Because they used the right magic pixie dust when they made the function. Which is great because if they had they used the wrong magic pixie dust then calling the function might have imploded your computer.

2. Don't use strtok.

3. Not really, no.

 

1. I did not understand your answer.

2. Others suggested regex. You suggest the same here ?

3. And, just why is it not a good question ? It is a basic & honest good question any good thinking newbie would have. Oh dear me! Sigh! ;)

Edited by phpsane

If you want to learn PHP, come up with your own examples instead of waiting for somebody to spoonfeed you. You've been provided with plenty of ideas and explanations, and the concept of splitting a string isn't exactly rocket science.

 

The confused-newbie game may work for a while, but at some point, it's time to grow up.

If you want to learn PHP, come up with your own examples instead of waiting for somebody to spoonfeed you. You've been provided with plenty of ideas and explanations, and the concept of splitting a string isn't exactly rocket science.

 

The confused-newbie game may work for a while, but at some point, it's time to grow up.

 

You really made me laugh, Jack! Anyway, how about a quizz ? That will be fun!

Anyone interested to play a little, may start a quizz in this thread.

Give us newbies 4 code samples, where 1 is totally incorrect, 1 is bad coding but not incorrect, 1 is ok coding and 1 is the best coding practice. We will try picking the best coding practice one. Let us see how well we fair! ;)

Quizzes will make the threads even more fun!

(Actually, I will try building a quizz script one day and turtles can climb all the way up the quizz ladder! Let us see how many new turtles manage to climb up the top!). ;)

Edited by phpsane

Yes, I know the manual shows examples but it won't show examples based on our discussion. Now, will it ? ;)

But an example based on your specific data wouldn't be for the other newbies you refer to. Defeats the object, doesn't it.

 

As it seem you will continue to bump the thread until someone writes the code for you, I'll save you the effort and close it now.

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.