PHP strtok ()

Source: Internet
Author: User
Tags strtok

// String strtok (string input, string separator );

/*
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.
*/

$ String = "This Is/tan example/nstring ";
$ Tok = strtok ($ string, "/n/t ");
While ($ Tok ){
Echo "word = $ Tok/N ";
Var_dump ($ Tok );
$ Tok = strtok ('/n/t'); // note it's not strtok ("/n/t ")
}
/*
Output:

WORD = This
String (4) "this"
WORD = is
String (2) "is"
WORD =
String (2) "A" // Why ???
WORD = example
String (7) "example"
WORD = // Why ???
S
String (2 )"
S"
WORD = ri
String (2) "Ri"
WORD = G
String (1) "G"
*/

/*
The behavior when an empty part was found changed with PHP 4.1.0. The old behavior returned an empty string, while the new, correct, behavior simply skips the part of the string:
*/

# Old strtok () behavior
$ First_token = strtok ('/something ','/');
$ Second_token = strtok ('/');
Var_dump ($ first_token, $ second_token );
/*
Output:

String (0 )""
String (9) "something"
*/

# New strtok () behavior
$ First_token = strtok ('/something ','/');
$ Second_token = strtok ('/');
Var_dump ($ first_token, $ second_token );
/*
Output:

String (9) "something"
Bool (false)
*/

?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.