Search code examples
phpstrtok

How can I use strtok in PHP?


Please can you explain to me why I get this result?

echo strtok('../../../its-2016-03/2016-11-15/c_00.01-rend.xml#eba_tC_00.01','-rend');

Result:

../../../its

It is pretty weird that I get this result.


Solution

  • From the manual:

    strtok() splits a string (str) into smaller strings (tokens), with each token being delimited by any character from token.

    Since you pass '-rend' as token, strtok returns your string up to the first character in the string which is also in token, in this case - and so the return value is '../../../its'.

    It's not clear exactly what you're trying to achieve, perhaps you want

    echo explode('-rend', '../../../its-2016-03/2016-11-15/c_00.01-rend.xml#eba_tC_00.01')[0];
    

    which will yield

    ../../../its-2016-03/2016-11-15/c_00.01