String functions in PHP- uses and examples

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.

Result:

Result:

str_word_count() -Word Count


str_word_count() function will give you the exact number of words that a string have.

Result:

Result:

strrev() -String Reverse


strrev() function is used to reverse the entire string.

Result:

trim() -String Remove


Remove whitespace (or other characters) from the beginning and end of a string

Output

Another example

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

strtoupper() -to upper case


This function converts the string in upper case

strtolower() -Converts string to lowercase


convert whole string into lowercase

chr() -Return character from ASCII code


returns any character or letter and also special character from their equivalent ASCII Code

implode() -array to string


Implode function combine all array element with a string

String- convert to string

Output

explode() -string to an array

Explode function split a string and return all value into an array.

Array- String to array conversion

Output

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.

Result:

str_replace() -Replace string

str_replace() function is used to replace some characters that are there in the strings.

Example:

Result