PHP String
Functions
strlen()
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!":
<?php
echo strlen("Hello world!"); // outputs 12
?>
str_word_count()
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!":
<?php
echo str_word_count("Hello world!"); // outputs 2
?>
strrev()
strrev() - Reverse a String
The PHP strrev() function reverses a string.
Example
Reverse the string "Hello world!":
<?php
echo strrev("Hello world!"); // outputs
!dlrow olleH
?>
strpos()
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!":
<?php
echo strpos("Hello world!", "world"); // outputs 6
?>
str_replace()
The PHP str_replace() function replaces some characters with some
other characters in a string.
Example
Replace the text "world" with "Dolly":
<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello
Dolly!
?>
PHP strtoupper() Function
Example
Convert all characters to uppercase:
<?php
echo strtoupper("Hello WORLD!");
?>
Output
HELLO WORLD!
PHP strtolower() Function
Example
Convert all characters to lowercase:
<?php
echo strtolower("Hello WORLD.");
?>
Output
hello world.
PHP strcmp() Function
Compare two strings (case-sensitive):
<?php
echo strcmp("Hello world!","Hello world!");
?>
<p>If this function returns 0, the two strings are equal.</p>
output
0
If this function returns 0, the two strings are equal
PHP ucwords() Function
The ucwords() function converts the first character
of each word in a string to uppercase.
Syntax
ucwords(string, delimiters)
<?php
echo ucwords("hello|world", "|");//displays Hello|
World
?>