A string is a sequence of characters, like “Hello world!”.
The PHP string functions are part of the PHP core. No installation is required to use these functions.
PHP has over 75 built-in String manipulation functions, supporting operations ranging from string repetition and reversal to comparison and search-and-replace.
Example to Get The Length of a String in PHP use strlen() function which returns the length of a string
<?php echo strlen("Hello world!"); ?>
Output: 12
Example to Count the Number of Words in a String using PHP str_word_count() function
<?php echo str_word_count("Hello world!"); ?>
Output: 2
Example to Reverse a String using PHP strrev() function
<?php echo strrev("Hello world!"); ?>
Output: !dlrow olleH
Example to Search for a Specific Text Within a String using PHP strpos() function
If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
The example below searches for the text “world” in the string “Hello world!”
Tip: The first character position in a string is 0 (not 1).
<?php echo strpos("Hello world!", "world"); // outputs 6 ?>
Output: 6
Example to Replace Text within a String using PHP str_replace() function
The example below replaces the text “world” with “Dolly”
<?php echo str_replace("world", "Dolly", "Hello world!"); // ?>
Output: Hello Dolly!