Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • some useful string functions in php

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 273
    Comment on it

    A sequence of characters is called string.There are lots of string functions which are commonly used in php to manipulate the string. Some example given as below

    strlen() function: Its used find out the length of the string i.e. total number of characters.

    <?php
    echostrlen("Hello Evontech!");
    // output: 15
    ?>
    

    strlen() is count whitespace  between  two the strings.

     

    str_word_count(): This function is used to count the total number words of a string.

    <?php
    echostr_word_count("Hello Evontech!");
    // output: 2
    ?>

     

    strrev(): It is used to find out the string as a reverse string. This function reads the string from last index of string array.

    <?php
    echostr_word_count("Hello Evontech!");
    // output:  !hcetnovE olleH
    ?>

     

    strpos(): It is used to search a  text within a string. It gives the starting character position of the search string, if text is found otherwise return false.

    <?php
    echostrpos("Hello Evontech!","Evontech");
    // outputs 6 because string index starting from zero
    ?>

     

    str_replace(): This is used to replace a text with in  the string by a given text.

    <?php
    echostr_replace("Evontech","Evon Tech","Hello Evontech!");
    // outputs "Hello Evon Tech!"
    ?>

     

    nl2br(): It is used to conversion for new line(\n) character to line break(<br>).

    <?php
    echo nl2br("Evon Tech.\n Findnerd.");
    //output: Evon Tech.
    //                Findnerd.
    ?>

     

    implode(): It is convert array to string.

    Syntax:
     implode(separator,array)
    separator: This is optional params, by default takes empty string
    array: required params which is convert to string

    <?php
    $array= array('Hello','Evon','Tech!');
    echo implode(" ",$array);
    //output: Hello Evon Tech! 
    ?>

     

    explode(): Its work unlike implode() and convert a string into array.

    <?php
    $str = "Hello Evon Tech";
    print_r (explode(" ",$str));
    ?>

     

 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: