1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Strings in PHP

Discussion in 'PHP' started by MinalS, Nov 15, 2015.

  1. Strings in PHP are similar to the ones used in other languages. They define a group of characters combined together to form a string. There are several string functions which can be easily used for data manipulation. User does not to perform separate installation.

    There are various representation of strings. The single quoted strings are defined as literals. Double quoted string are defined as variables whose values can be easily replaced.

    Example:

    Code:
    <html>
        <body>
            <?php
                $var = "age";
                $literal = ‘Welcome and learn PHP’;
                echo ($literal);
                echo "<br/>";
                $literal = "Harry completed $var years";
                echo ( $literal);
            ?>
        </body>
    </html>
    
    User can delimit the string using double quotes. User can execute the string in two different ways as:
    1. The character sequence starting with the backslash ( \ ) is modified with the special character
    2. The names of the variables are replaced with the string values
    The various escape sequence replacements used in PHP are as mentioned below:
    1. The \r character is replaced with the carriage return
    2. The \$ replaced the dollar sign
    3. The \n replaces the newline character
    4. The \t defines the tab character
    5. \\ is changed with the single backslash
    6. The \" is modified with the single double quote

    String functions



    Let’s explore some of the string functions used for manipulating the strings in PHP. For complete list of string functions in PHP refer PHP.net here

    1. Length of the string

    The strlen() function in PHP is used for manipulating the strings in PHP. The string is passed as the parameter to the function.

    Example:
    Code:
    <html>
        <body>
            <?php
                echo strlen ("Welcome");
            ?>
        </body>
    </html>
    
    2. Reverse of the string

    The strrev() function reverses the string defined in PHP.

    Example:
    Code:
    <html>
        <body>
            <?php
                echo strrev ("Welcome");
            ?>
        </body>
    </html>
    
    3. Covert the string to lowercase

    The characters of the string are converted into lowercase.
    Example:
    Code:
        <html>
        <body>
            <?php
                echo strtolower ("Welcome");
            ?>
        </body>
    </html>
    
    4. Convert the string to uppercase

    The characters of the string are converted into uppercase.
    Example:
    Code:
        <html>
        <body>
            <?php
                echo strtoupper ("Welcome");
            ?>
        </body>
    </html>
    
    5. Return the part of the string

    The substr() function is used for returning the part of the string. The parameters passed to the function are
    1. 1) string: The part of the string is returned
    2. 2) start: The position from where the string is starting
    3. 3) length: The length of the string is returned
    Example:

    Code:
    <html>
        <body>
            <?php
                echo substr ("Welcome User", 9)."<br>";
                echo substr ("Welcome", 3)."<br>";
                echo substr ("Welcome", 6)."<br>";
            ?>
        </body>
    </html>
    
    6. The position of the element in the string

    The strrpos() function is used to define the position of the occurrence of an element inside the string.

    The function consists of three parameters as mentioned below:
    1. 1) string: The string to be searched is defined
    2. 2) find: The string to be found is specified
    3. 3) start: Defines where to start the search

    Example:

    Code:
    <html>
        <body>
            <?php
                echo strrpos ("understand PHP, Learn understand PHP", "PHP");
            ?>
        </body>
    </html>
    
    7. Compare the two strings

    The strcmp() function is used for comparing the two strings.

    Example:

    Code:
    <html>
        <body>
            <?php
                echo strcmp ("Welcome User!!", "Welcome User!");
            ?>
        </body>
    </html>
    
    8. using the trim() function

    It is used for removing the whitespaces and other characters from the sides of the string.

    Example:

    Code:
    <html>
        <body>
            <?php
                $str1 = "Welcome";
                echo $str1. "<br>";
                echo trim ($str, "Wee");
            ?>
        </body>
    </html>
    
     

Share This Page