★ wanayoo — archive 1999 http://www.php.net/manual/function.strtok.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Strings
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Strings
* AddCSlashes
* AddSlashes
* bin2hex
* Chop
* Chr
* chunk_split
* convert_cyr_string
* count_chars
* crc32
* crypt
* echo
* explode
* flush
* get_html_translation_table
* get_meta_tags
* hebrev
* hebrevc
* htmlentities
* htmlspecialchars
* implode
* join
* levenshtein
* ltrim
* md5
* Metaphone
* nl2br
* ob_start
* ob_get_contents
* ob_end_flush
* ob_end_clean
* ob_implicit_flush
* Ord
* parse_str
* print
* printf
* quoted_printable_decode
* quotemeta
* rtrim
* sscanf
* setlocale
* similar_text
* soundex
* sprintf
* strcasecmp
* strchr
* strcmp
* strcspn
* strip_tags
* stripcslashes
* stripslashes
* stristr
* strlen
* str_pad
* strpos
* strrchr
* str_repeat
* strrev
* strrpos
* strspn
* strstr
* strtok
* strtolower
* strtoupper
* str_replace
* strtr
* substr
* substr_count
* substr_replace
* trim
* ucfirst
* ucwords
Manual: strtok
View the source code for this pageSearch the site



Previous page
 strstr
 Updated
Sat, 12 Aug 2000
strtolower 
Next page


strtok

(PHP3 , PHP4 )

strtok -- Tokenize string

Description

string strtok (string arg1, string arg2)

strtok() is used to tokenize a string. That is, if you have a string like "This is an example string" you could tokenize this string into its individual words by using the space character as the token.

Example 1. Strtok() example


$string = "This is an example string";
$tok = strtok ($string," ");
while ($tok) {
    echo "Word=$tok<br>";
    $tok = strtok (" ");
}
      

Note that only the first call to strtok uses the string argument. Every subsequent call to strtok only needs the token to use, as it keeps track of where it is in the current string. To start over, or to tokenize a new string you simply call strtok with the string argument again to initialize it. Note that you may put multiple tokens in the token parameter. The string will be tokenized when any one of the characters in the argument are found.

Also be careful that your tokens may be equal to "0". This evaluates to false in conditional expressions.

See also split() and explode().


User Contributed Notes: strtok


pajoye@joye.org
18-May-1999 07:38
strtok doenst work when you pass an element of a string array (result from split b.e.). You have to copy the content to a "standart" string variable.
I ve tested it on Windows NT4.0 SP4. Didnt test on Linux or others system. The php has been installed from the binary version on www.php.net.



amit@affinity.net
16-Jun-1999 01:22
The documentation for this function says that it allows strings to be passed as the delimiters but ITS WRONG! The only thing that it allows as the delimiter is a character, so be careful.


abhijit@mds.rmit.edu.au
30-Nov-1999 05:46
If you want to split a string using a "string" as the delimiter- try explode. Works great!



kgrose@web.net
02-Dec-1999 05:03
If you are trying to use STRTOK to tokenize large strings (like a couple of formatted paragraphs) stored in a single variable, don't. The STRTOK function stops recursing after reading a newline character in the variable.


kgrose@web.net
02-Dec-1999 05:53
My post about newline character was wrong. It's not the newline that causes strtok to stop working, it's if it runs into whatever character you've specified as the delimiter several times in a row (like more than 2 spaces if space is your delimiter).


tq@justkidding.com
12-Feb-2000 11:03
strtok seems to have some persistent info stored invisibly. I tried to use it to parse two strings by alternating calls with each string. It completely mixed up the results. I had to entirely parse the strings separately and store the results. Then it worked ok. This is probably a bug.


ridcully@gmx.li
07-Mar-2000 04:51
It's not a bug. As mentioned in the documentation, strtok() takes the string to be tokenized only when called the first time. It stores a pointer to the string internally and subsequent calls to strtok() (without the string parameter) use that pointer.


mckay@et.byu.edu
03-May-2000 07:43
Note that php's strtok() is NOT the same as the C library function strtok(). From the documentation on the C library function:
strtok(s1,s2) considers the string s1 to consist of a sequence of
zero or more text tokens separated by spans of one or more
characters from the separator string s2.

Replace "one or more" above with "exactly one" and you'd have a description of what php's strtok() does. Thus, to php's strtok, "three spaces here" maps to the tokens "three" "spaces" "" "" "here". The C library function (much more useful) would map the same string to the tokens "three" "spaces" "here".

I'm using php version 3.1.12--perhaps this will be changed in later versions



domidahl@gmx.net
10-May-2000 09:08
If you tokenize a string containing "0" use strlen($tok) as breaking-condition of the loop.
This works because strval(false) is "".



vksgeneric@hotmail.com
12-Jul-2000 03:11
a lot of people implied it by now, but nobody wrote the magic solution. If you have, for example, a couple delimiters following each other, the example in this chapter:
<pre>
$tok = strtok ($string," ");
while ($tok) {
...
</pre>
will fail. Instead, try something like this:
<pre>
$tok = strtok ($string, " ");
while(!($tok === FALSE)){
...
</pre>
(Yes, this is three '=' in a row...)



 About Notes


Previous page
 strstr
 Updated
Sat, 12 Aug 2000
strtolower 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.