PHP

An analysis of the advantages of the PHP strtok of function


Its advantages are:

1. You can define more than one delimiter at a time. The function is cut by a single delimiter, not by the entire delimiter, and the limit is cut by the entire delimiter string. Therefore, arrow can be cut in Chinese, but strtok can’t.

2. During strtok() traversal using while or for, the separator can be changed at any time, and the break can be used to terminate the cutting at any time.

Example 1: demonstrates how to cut with restraint in Chinese

$string = “this is PHP forum forum forum section forum column forum H administrator forum member”; $arr = explode(” forum ”,$string); The foreach ($arr as $v) { Echo $v. ”< Br / >” ; } Echo ”— — — — — — — — — — — — — < Br / >” ;

Returns:

This is a PHP

section The column H the administrator members -------------

Example 2: demonstrates the replacement of the cutters, noting that there is no longer an “H” separator in the WHILE. I’m just going to use Spaces.

$string = “this is PHP forum forum forum section forum column forum H administrator forum member”; $tok = strtok($string, “H”); / / space + H $n = 1; While ($tok! = = false) { Echo $tok<” Br / >” ; $tok = strtok (” ”); / / space / / if ($n> 2) break; // can jump out at any time. / / $n++; } Echo ”— — — — — — — — — — — — — < Br / >” ;

Returns:

This is a P P BBS BBS section BBS program Forum H administrator BBS member -------------

Example 3: demonstrate multiple delimiters.

$string = “This is\tan example\nstring”; $tok = strtok($string, “\n\t”); # space, line break, TAB While ($tok! = = false) { Echo $tok<” Br / >” ; $tok = strtok (” \ n \ t ”); } Echo ”— — — — — — — — — — — — — < Br / >” ;

Returns:

This is The an example The string -------------

$string = “abcde 123c4 99sadbc99b5232”; $tok = strtok ($string, “BC”); While ($tok! {= "") Echo $tok<” Br / >” ; $tok = strtok (” BC ”); } Echo ”— — — — — — — — — — — — — < Br / >” ;

Returns:

a. DE 123 4 99 sad 99 5232 -------------

Example 4: demonstrate using for to traverse:

$line = “leon\tatkinson\[email protected]”; For ($token = strtok ($line, ”\ t”); $token! = ""; $token = strtok (” \ t ”)) { Print (” token: $token< BR> \ n ”); }

Returns:

Token: Leon Token: atkinson Token: [email protected]