Whats The use of function strtok() in PHP, how is better than other string function doing the same thing?
Asked Answered
B

4

27

Whats the use of function strtok() in PHP? How is better than other string function doing the same thing?

Belcher answered 27/3, 2010 at 4:43 Comment(1)
T
41

There is no other string function that does the same thing. Functions like explode return the complete split string in an array. strtok on the other hand only returns one piece at a time, with each subsequent call returning the next piece. This is potentially much more economic and memory preserving for large strings.

Tzong answered 27/3, 2010 at 4:52 Comment(0)
K
7

strtok is preferred over other function when you have to split a string on more than one delimiter.

The other function that this like preg_split, split are regex based and will have associated overhead with them.

Kobylak answered 27/3, 2010 at 4:54 Comment(0)
C
6
$addr=strtok($_SERVER['REQUEST_URI'],'?');

but do not call it "better". it's just another way to do it.

Cretan answered 27/3, 2010 at 4:51 Comment(0)
K
5

strtok() is a great and very useful function but:

  • This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE (PHP Manual).

  • If you want to tokenize by only one letter, explode() is much faster compared to strtok() (Source).

  • If you have memory-usage critical solution, you should keep in mind, that strtok function holds input string parameter (or reference to it?) in memory after usage (Source).

Kronick answered 12/6, 2017 at 10:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.