php 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 =  a
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)
*/

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值