strtok() rationale

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I downloaded a few PHP scripts from various sites, in part to use them
as is, and partly to study and learn from them.

One script is littered with strtok() occurrences. So I checked the
manual for its details.
I do understand the basics of it, and the limitations (like not being
able to work on two strings simultaneously for loss of the strtok
internal 'pointer').

And it MUST be my ignorance and/or limited experience, but I don't
understand what this function has to offer that can't be solved with
str_split and explode functions, or to be more precise, what typical
problem can better/easier/faster be solved by using strtok() than by
the 'easier' string and array functions.

I am convinced there are good reasons to use strtok, maybe some folks
here can point me to a 'textbook situation' where strtok saves the day
or copy/paste a good self-explanatory example or link to additional
strtok() reading material.


Re: strtok() rationale

On 26 May 2007 12:43:24 -0700, wrote:

Quoted text here. Click to load it

 It's possible that the author of the code has experience writing string
manipulation code in C, and so was drawn to PHP's string functions that mirror
the standard ones from <string.h>, including strtok, which also inherits the C
function's limitations (most notably, no requirement for reentrancy).

Andy Hassall :: :: :: disk and FTP usage analysis tool

Re: strtok() rationale wrote:
Quoted text here. Click to load it

strtok and other strxxx functions from C's standard library have the  
advantage of speed and smaller memory footprint. For example if you have  
a comma-separated list of 1000 elements,

$first = strtok($list, ',')

is apparently much better than

list($first) = explode(',', $list);

Of course, in most real life situations such optimizations are not worth  
the trouble, finally it's only a scripting language... but strtok and  
friends are a good option for those who want fastest possible code.

gosha bine

extended php parser ~
blok ~

Re: strtok() rationale

Quoted text here. Click to load it

Is this a shortcut of sorts to memory reference pointers? It feels a
little bit like a string_map function. Scripters have a hard time
molding that problem.

I think it's interesting that in some of the newer languages, the
simplest constructs are abstract ideas that similarly describe the
most systemically atomic memberships: generics, collections, objects.
Scripting is built around flexibility, so everything is everything;
ie, in JavaScript, everything is a generically collectionable object
(consider closures and anonymous functions); once you figure out how
to build classes and constructors, it's like gravy. C#, php, java,
python, etc...

"Simplicity" points at something different in strong-typed languages
than it does in soft-typed interpreted languages.

Site Timeline