PHP Strings


The alphabet unit is a sequence of letters, such as "Hello world!".


PHP String Functions

In this article we will look at some commonly used methods to deceive ropes.


strlen() - Return the Length of a String

The PHP strlen() function returns the length of the character unit.


Example

Return the length of the string "Hello world!":

<?php
echo strlen("Hello world!"); // outputs 12
?>


str_word_count() - Count Words in a String

The PHP function str_word_count() counts the number of words in a character unit.


Example

Count the number of word in the string "Hello world!":

<?php
echo str_word_count("Hello world!"); // outputs 2
?>


strrev() - Reverse a String

The PHP strrev() function loosens the thread.


Example

Reverse the string "Hello world!":

<?php
echo strrev("Hello world!"); // outputs !dlrow olleH
?>


strpos() - Search For a Text Within a String

The PHP strpos() function searches for specific text within a character unit. When the same is found, the function restores the character of the original game character. 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
?>

Tip:The position of the first letter in a series is 0 (not 1).


str_replace() - Replace Text Within a String

The PHP str_replace() function replaces characters with other characters in the series.


Example

Replace the text "world" with "Dolly":

<?php
echo str_replace("world", "Dolly", "Hello world!"); // outputs Hello Dolly!
?>