String function in PHP

PHP string function
  • Strings are the collection of characters.
  • A string holds letters or any alphabets, numbers, and even special characters, so the String is a non-numeric data type.
PHP String Functions
  • In this topic, we will discuss commonly used functions to manipulate strings.
  • strlen()
  • The PHP strlen() function returns the length of a string.

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo "length of string : " . strlen("Hello PHP Coders");
    ?>
    
    </body>
    </html>
    
  • str_word_count()
  • The PHP str_word_count() function counts the number of words in a string.

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo "Word in string is : " . str_word_count("Hello PHP Coders");
    ?>
    
    </body>
    </html>
    
  • strrev()
  • The PHP strrev() function reverses a string.

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo "Reverses string is : " . strrev("Hello world!");
    ?>
    
    </body>
    </html>
    


  • strpos()
  • The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo "position is : " . strpos("Hello PHP coders!", "coders");
    ?>
    
    </body>
    </html>
    
  • str_replace()
  • The PHP str_replace() function replaces some characters with some other characters in a string.

    <!DOCTYPE html>
    <html>
    <body>
    
    <?php
    echo str_replace("world", "php coders", "Hello world");
    ?>
    
    </body>
    </html>
    

    Exercise: Other PHP string function list click here for see list.