Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Use of strtok() function in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 332
    Comment on it

    It is basically the string function and its used to split the string into smaller strings but other than this we have also other string functions to split the string like explode (return the complete split string in an array).

    But strtok is different to the other string functions which is also used to split the string. Its basically only returns one piece of string at a time.

    This is potentially much more economic and memory preserving for large strings.

    It is preferred over other function when you have to split a string on more than one delimiter.

    The syntax of this given below:

    <p>Syntax:</p>
    strtok(string,split)
    

    Below is the basic code to describe the strtok() function in php.

    <html>
    <body>
    <?php
    $string = "Hello world. Beautiful day today.";
      $token = strtok($string, " ");
    
    while ($token !== false)
        {
        echo "$token<br>";
        $token = strtok(" ");
        }
    ?>
    </body>
    </html>
    

    Here, the above code consider the string which contain the dummy text string and then use the string function strtok() to split the given string into the smaller sections.

    And, the below is the result of the above code after split the string.

    <p>Result:</p>
    Hello
    world.
    Beautiful
    day
    today
    
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: