



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
PHP Strings.........................................................
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!
A string is a sequence of characters, like "Hello world!".
PHP String Functions
strlen() - Return the Length of a String
The PHP strlen() function returns the length of a string.
Example
Return the length of the string "Hello world!":
str_word_count() - Count Words in a String
The PHP str_word_count() function counts the number of words in a string.
Example
Count the number of word in the string "Hello world!":
echo str_word_count("Hello world!"); // outputs 2?>
strrev() - Reverse a String
The PHP strrev() function reverses a string.
Example
Reverse the string "Hello world!":
echo strrev("Hello world!"); // outputs !dlrow olleH?>
strpos() - Search For a Text Within a String
The PHP strpos() function searches for a specific text within a string. If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
Example
Search for the text "world" in the string "Hello world!":
echo strpos("Hello world!", "world");// outputs 6?>
str_replace() - Replace Text Within a String
The PHP str_replace() function replaces some characters with some other characters in a string.
Example
Replace the text "world" with "Sachin":
echo str_replace("world", "Sachin", "Hello world!"); // outputs Hello Sachin!?>
ucfirst()
The ucfirst() function returns string converting first character into uppercase. It doesn't change the case of other characters.
Example
$str="php programming";$str=ucfirst($str);
echo $str;
?>
Output:
Php programming
lcfirst()
The lcfirst() function returns string converting first character into lowercase. It doesn't change the case of other characters.
Example
$str="PHP Programming";$str=lcfirst($str);
echo $str;
?>
Output:
pHP Programming
ucwords()
The ucwords() function returns string converting first character of each word into uppercase.
Example
$str="php programming";$str=ucwords($str);
echo $str;
?>
Output:
Php Programming