Become Our Fan on Social Sites!

Facebook Twitter

Google+ RSS YouTube

Monday 16 December 2013

String functions in php

There are many string functions, below PHP script illustrates six php string functions: 1) strlen 2) strpos 3) chr 4) explode 5) implode 6) str_replace



Output:

Length of PHP: 3
strpos: 9
chr: A
explode: Array ( [0] => Personal [1] => Home [2] => Page )
implode: Personal-Home-Page
str_replace: Hello Jignesh

 Source Code:


<?php

    // strlen function returns length of given string.    
    echo "Length of PHP: ".strlen("PHP")."<br />";

    // strpos function is used to get position of perticular
    // character or words. If match not found then it returns
    // FALSE. Position is return based on 0.
    echo "strpos: ".strpos("Personal Home Page","Home")."<br />";

    // chr function returns character of specified ASCII value
    echo "chr: ".chr(65)."<br />";

    // explode function breaks a string into an array
    // In our example, "Personal" stored in 0th position,
    // "Home" stored in 1st position and "Page" stored in 
    // 2nd position.
    echo "explode: ";
    print_r(explode("-","Personal-Home-Page"));
    echo "<br />";

    // implode function returns string from 
    // the elements of an array
    $arr = array("Personal","Home","Page");
    echo "implode: ".implode("-",$arr)."<br />";

    // str_replace function replaces some characters 
    // with some other characters in a string.
    // This function is case-sensitive. Use str_ireplace 
    //function to perform case-insensitive search.
    echo "str_replace: ".str_replace("world","Jignesh","Hello world");

?>


0 comments :

Post a Comment