How to extract substring from a string in PHP?

How to extract substring from a string in PHP?

 

Asked on November 1, 2018 in PHP.
Add Comment


  • 1 Answer(s)

    Here the PHP substr() function can be used.
        The PHP substr() function can be used to get the substring i.e the part of a string from a string.
    This function takes the start and length parameters to return the portion of string.

    For example:

    <?php
    $str = "Hello World!";
    echo substr($str, 0, 5); // Outputs: Hello
    echo substr($str, 0, -7); // Outputs: Hello
    echo substr($str, 0); // Outputs: Hello World!
    echo substr($str, -6, 5); // Outputs: World
    echo substr($str, -6); // Outputs: World!
    echo substr($str, -12); // Outputs: Hello World!
    ?>
    
    Answered on November 1, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.