Now list some String functions in PHP to handle and manipulate string in PHP.
All functions explained are inbuilt PHP functions also known as predefined string functions in PHP.
These functions are defined during creation of PHP.
sterlen() Length of String
sterlen() function is used to get the entire length of the string. If there is space in between two words then it will count that space as well.
1 2 3 | <?php Echo strlen(“India”); ?> |
Result:
1 | 5 |
1 2 3 | <?php echo strlen("Hello world!"); ?> |
Result:
1 | 12 |
str_word_count() -Word Count
str_word_count() function will give you the exact number of words that a string have.
1 2 3 | <?php echo str_word_count(“ Paris is beautiful city”); ?> |
Result:
1 | 4 |
1 2 3 4 5 | <?php $s =" GOOD DAY MISS MOLLY! "; $res =str_word_count($s); echo $res; ?> |
Result:
1 | 4 |
strrev() -String Reverse
strrev() function is used to reverse the entire string.
1 2 3 | <?php Echo strrev(“paris”); ?> |
1 2 3 | <?php echo strrev("Hello world!"); ?> |
Result:
1 | !dlrow olleH |
trim() -String Remove
Remove whitespace (or other characters) from the beginning and end of a string
1 2 3 4 5 6 7 | <?php $text = " These are a few words :) ... "; echo $text; echo "<br/>"; $res=trim($text," "); echo $res; ?> |
Output
1 2 | These are a few words :) ... These are a few words :) ... |
Another example
1 2 3 4 5 6 7 | <?php $str = "I am using Trim"; echo $str; echo "<br/>"; $without_space = rtrim($str,"im"); echo $without_space; ?> |
1 2 | I am using Trim I am using Tr |
ltrim() => Remove whitespace (or other characters) from the beginning of a string
rtrim() => Remove whitespace (or other characters) from the end of a string
ucfirst()- Convert first letter into uppercase
convert first letter into uppercase of any string
1 2 3 4 | <?php $value = "john smith"; echo ucfirst($value); ?> |
1 | John smith |
strtoupper() -to upper case
This function converts the string in upper case
1 2 3 4 5 | <?php $s="good day miss molly!"; $r=strtoupper($s); echo $r; ?> |
1 | GOOD DAY MISS MOLLY! |
strtolower() -Converts string to lowercase
convert whole string into lowercase
1 2 3 4 5 | <?php $s="GOOD DAY MISS MOLLY!"; $r=strtolower($s); echo $r; ?> |
1 | good day miss molly! |
chr() -Return character from ASCII code
returns any character or letter and also special character from their equivalent ASCII Code
1 2 3 | <?php echo chr(65); ?> |
1 | A |
implode() -array to string
Implode function combine all array element with a string
1 2 3 4 5 6 | <?php $arr=array(10,20,35, 'aaa',99, 'ccc'); echo "<h1>Array</h1><br>"; print_r ($arr); print " "; $string=implode(" ",$arr); |
String- convert to string
1 2 3 4 5 6 7 8 9 10 11 12 | <?php <?php $arr=array(10,20,35, 'aaa',99, 'ccc'); echo "<h1>Array</h1><br>"; print_r ($arr); print " "; $string=implode(" ",$arr); print "<br/>"; echo $string; ?> ?> |
Output
1 2 3 4 | Array Array ( [0] => 10 [1] => 20 [2] => 35 [3] => aaa [4] => 99 [5] => ccc ) 10 20 35 aaa 99 ccc |
explode() -string to an array
Explode function split a string and return all value into an array.
1 2 3 4 5 6 7 8 | <?php $s="c,c++,java,HTML"; echo "<h1>STRING</h1>"; echo $s; //output print "<hr/>"; $arr=explode(",",$s); print_r($arr); ?> |
Array- String to array conversion
1 2 3 4 5 | STRING <?php $arr = array('c','cpp','java','php'); echo implode(" ",$arr); ?> |
Output
1 | c cpp java php |
strpos() -find the text position in string
strpos() function is used to find the particular text in a string.
If while searching in the string it founds the text, then it will give the position of that word in first search match or if the particular text is not found then it will return FALSE value.
Also first position of any character in string is 0 not 1.
1 2 3 | <?php echo strpos("paris is beautiful city", "city"); ?> |
Result:
1 | 19 |
str_replace() -Replace string
str_replace() function is used to replace some characters that are there in the strings.
Example:
1 2 3 | <?php echo str_replace("Paris", "London", "Paris is beautiful city"); ?> |
Result
1 | London is beautiful city |