Sunday, 9 June 2013

INBUILT STRING MANIPULATION FUNCTIONS IN PHP

Here are some inbuilt string functions in php which will help you to do enough manipulation with the  strings.
Source code:-
/*******************************************************************/
<html>
                <head>
                                <title>Inbuilt string functions</title>
                </head>
               
                <body>
                <?php
                                $str1="just stay on the problem";
                                $str2=" a bit longer than others";
                               
                                echo "string 1 = ".$str1."<br />";
                                echo "string 2 = ".$str2."<br />";
                               
                                /*to convert the whole stirng to upper case*/
                                echo "str to upper = ".strtoupper($str1)."<br />";
                               
                                /*to convert the whole string to lower case*/
                                echo "str to lower = ".strtolower($str2)."<br />";
                               
                                /*to find the length of string*/
                                echo "length of string = ".strlen($str1)."<br />";
                               
                                /*to make first character of string uppercase */
                                echo "ucfirst = ".ucfirst($str1)."<br />";
                               
                                /* to make first characters of all the words in string uppercase*/
                                echo "ucword = ".ucwords($str1)."<br />";
                               
                                /*to repeat the specified string 5 times */
                                echo "str_repeat = ".str_repeat($str1,5)."<br />";
               
                                /*to form a sub string from the main string*/
                                echo "substr = ".substr($str1,5,15)."<br />";
                               
                                /*to find the index number of a sub string in main string*/
                                echo "strpos = ".strpos($str1,"on")."<br />";
                               
                                /* to replace "the" from "your" */
                                echo "str_replace = ".str_replace("the","your",$str1)."<br />";
                               
                                /* to find sub string inmain string and then print all
                                the characters following the sub string from main string*/
                                echo "strstr = ".strstr($str1,"the")."<br />";
                               
                                /* to find a character in main string and then print all
                                the characters following that character from the main string*/
                                echo "strchr = ".strchr($str1,"t")."<br />";
                               
                                /* to concatenate two strings ignoring space if the second
                                string contains a blank space as its first character */
                                echo "trim = ".$str1.trim($str2);
                ?>
                </body>
</html>

/******************************************************************/

Output:-

string 1 = just stay on the problem
string 2 = a bit longer than others
str to upper = JUST STAY ON THE PROBLEM
str to lower = a bit longer than others
length of string = 24
ucfirst = Just stay on the problem
ucword = Just Stay On The Problem
str_repeat = just stay on the problemjust stay on the problemjust stay on the problemjust stay on the problemjust stay on the problem
substr = stay on the pro
strpos = 10
str_replace = just stay on your problem
strstr = the problem
strchr = t stay on the problem
trim = just stay on the problema bit longer than others

No comments:

Post a Comment