In the last article, we saw some of the most commonly used built-in functions for array manipulation in PHP. This article throws light on some of built-in PHP functions that can be used for string manipulation. Process of defining and using string has already been explained in the earlier articles. This article solely focuses on built-in string manipulation functions in PHP.
strlen()
Returns the number of characters in a string.
trim()
This function removes white spaces from the beginning and end of the string.
ltrim()
This function removes white spaces from the start of the string
rtrim()
This function removes white spaces from the end of the string.
strtolower()
Converts all the characters in the string to lowercase.
strtoupper()
Converts all the characters in the string to uppercase.
substr()
Return a part of the string depending upon the passed parameters.
str_replace()
Use to replace part of string with some other string
explode()
Use to break a string into array depending upon the delimiter.
implode()
Use to join items in the array to a single string.
Have a look at the following example to see how these functions work
" ; // Converting string to upper case $string2 ="Shane lee"; echo strtoupper($string2); echo "" ; // Getting substring $string3 = substr("Wizard", 1, 2); echo $string3; echo "" ; // Replacing a with e in Wizard $string3 = str_replace("a", "e", "Wizard"); echo $string3; echo "" ; //Exploding a string based on comma $stringarr = explode(",", "My, Name, is, Mike"); print_r($stringarr); echo "" ; // Join array elements echo implode(",",$stringarr); echo "" ; // Getting length of a string echo strlen("Knowledge hills is awesome."); // 27 characters echo "" ; // Triming string from start to end echo "" ; echo trim(" Hello to Knowledgehills "); ?>