|
Posted by gosha bine on 05/26/07 22:50
semi_evil@inbox.com wrote:
> 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.
>
> Cheers!
> Semi
>
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 ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
[Back to original message]
|