PHP Tutorial


PHP 7 String Functions

Author:- w3alert.com
Updated Date:-01-02-2020

In this tutorial, We will discuss some basic and daily useful string functions in PHP which using in PHP script.

PHP String Function is used to access and manipulate Strings.

Some PHP basic string functions are display here,

PHP String Length Function: strlen()

The strlen() function is the pre-built function, that is used to calculate the length of a string.

Syntax:

    strlen( $string );

Example of PHP String Length Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "This function is used to calculate the length of a string.";

    echo strlen( $string );

    ?> 
     
    </body>
    </html>

PHP String Word Count Function: str_word_count()

The str_word_count function is used to count the number of the word in a string.

Syntax:

    str_word_count( $string );

Example of PHP String Word Count Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "This function is used to count the word in a string.";

    echo str_word_count( $string );

    ?> 
     
    </body>
    </html>

PHP String Reverse Function: strrev()

A string is reversed using the string reverse function provided by PHP.

Syntax:

    strrev( $string );

Example of PHP String Reverse Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " it is used to reverse a string. ";

    echo strrev( $string );

    ?> 
     
    </body>
    </html>

PHP String Position Function: strpos()

This strpos() function checks for a specific word in a string and if a matching word is found in the string, the function returns the position of the string. Returns false if no matching word is found.

Syntax:

   strpos( $string );

Example of PHP String Position Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "This is a string.";

    echo strpos( $string , "string" );

    ?> 
     
    </body>
    </html>

PHP String Replace Function: str_replace()

PHP str_replace() function is used to replace some characters into a string with other characters.

Syntax:

    str_replace( $searchValue, $replacValue, $subject_string );

Example of PHP String Replace Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " This is a string. ";

    echo str_replace( "string" , "string replace function" , $string );

    ?> 
     
    </body>
    </html>

PHP String to Uppercase Function: strtoupper()

PHP strtoupper() function converts string characters to uppercase.

Syntax:

    strtoupper( $string );

Example of PHP String to Uppercase

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " Hello, My Dear FrienDs. ";

    echo strtoupper( $string );

    ?> 
     
    </body>
    </html>

PHP String to Lowercase: strtolower()

PHP strtolower() function is used to converts string characters to uppercase.

Syntax:

    strtolower( $string );

Example of PHP String to Lowercase

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " HELLO,MY DEAR FRIENDS. ";

    echo strtolower( $string );

    ?> 

    </body>
    </html>

PHP String Uppercase First Function: ucfirst()

The PHP ucfirst () function as the name suggests, that function is used to convert the first letter of a given string to uppercase.

Syntax:

    ucfirst( $string );

Example of PHP String Uppercase First Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "hello john, how are you ?";

    echo ucfirst( $string );

    ?> 
     
    </body>
    </html>

PHP String Lowercase First Function: lcfirst()

PHP String Lowercase First Function which is used to convert the lowercase first letter of a string.

Syntax:

   lcfirst( $string );

Example of PHP String Lowercase First Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "HELLO JOHN";

    echo lcfirst( $string );

    ?> 
     
    </body>
    </html>

PHP Uppercase Words Function: ucwords()

PHP uppercase word function is used to convert the first letter of all words in a string to uppercase.

Syntax:

    ucwords( $string );

Example of PHP Uppercase Words Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " welcome to w3alert";

    echo ucwords($string);

    ?> 
     
    </body>
    </html>

PHP Lowercase Words Function: lcwords()

PHP uppercase word function is used to convert the first letter of all words in a string to lowercase.

Syntax:

    lcwords( $string );

Example of PHP Lowercase Words Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " Welcome To W3alert";

    echo lcwords($string);

    ?> 
     
    </body>
    </html>

PHP Echo Function: echo()

PHP provides an echo function used to display the output of more than one string.

Syntax:

    echo($string);

Example of PHP Echo Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " It is used to show output of the string.";

    echo ( $string );

    ?> 
     
    </body>
    </html>

PHP Print Function: print()

PHP Print Function is also used to display output of more than one string but it is slower than echo() function.

Syntax:

   print ( $string );

Example of PHP Print Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " It is also used to show output of the string.";

    print ( $string );

    ?> 
     
    </body>
    </html>

PHP Right Trim Function: rtrim()

PHP trim() function is pre-built function, that is used to remove whitespace right side a string. and also pre-defined characters.

Syntax:

    rtrim( $string );

Example of PHP Right Trim Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " Welcome to          w3alert.com ";

    echo rtrim($string);

    ?>

    </body>
    </html>

PHP Left Trim Function: ltrim()

PHP trim() function is the pre-built function, that is used to remove whitespace left side a string. and also pre-defined characters.

Syntax:

    ltrim( $string );

Example of PHP Left Trim Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php
    $string = "          Welcome to w3alert.com ";

    echo ltrim( $string );
    ?>

    </body>
    </html>

PHP Trim Function: trim()

PHP trim() function is the pre-built function, that is used to remove whitespace both sides (left and right sides) a string. and also pre-defined characters.

Syntax:

    trim( $string );

Example of PHP Trim Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "      Welcome to w3alert.com        ";

    echo trim( $string );

    ?>

    </body>
    </html>

PHP MD5 Message-Digest Algorithm Function: md5()

This function is used for security purposes. The MD5 function uses RAS data security (RSA digital risk management and cybersecurity). In PHP, the MD5 message-digest algorithm function takes the used string and converts the given string / message-digest into 128-bit RFC 1321 as output.

Syntax:

   md5( $string );

Example of PHP MD5 Message-Digest Algorithm Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = "this is my password";

    echo md5($string);

    ?>  
     
    </body>
    </html>

PHP String Repeat Function: str_repeat

str_repeat function is a pre-built function in PHP which is used to repeat a string by the given number of times.

Syntax:

    str_repeat( $string, $given_number );

Example of PHP String Repeat Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " He is a good developer." ;

    echo str_repeat($string,3);

    ?>

    </body>
    </html>

PHP Implode Function: implode()

The PHP implode function is used to create a string by elements present in an array.

Syntax:

    implode( $array, delimeter );

Example of PHP Implode Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $array = array( "This" , "is" , "Implode" , "Function." );

    echo implode( $array, " ");

    ?>

    </body>
    </html>

PHP Explode Function: explode()

The PHP explode () function is used to convert a string into an array by the provided delimiter.

Syntax:

    explode( delimeter, $array );

Example of PHP Explode Function

    <!DOCTYPE html>
    <html>
    <body>

    <?php

    $string = " This|is|Implode|Function. ";

    print_r (explode( "|", $string ));

    ?> 

    </body>
    </html>