A string is a group of characters joined together like “I love programming”.
- 0.1 Functions in PHP Strings
- 1 PHP Constants
Functions in PHP Strings
There are several PHP Strings functions that are used by programmers.
Function to get String Length
PHP strlen() function is used to get the length of a string.This function returns the string length in forms of a value.
Below example returns the length of “I love Programming ”
<?php echo strlen("I love Programming "); // The outputs is 16 ?>
See also PHP Data types
Function to count number of words that a string has
str_word_count() is the function used to return the number of words that a string has.
See Below Example
<?php echo str_word_count("I love Programming "); // The outputs is 3 ?>
Function to Reverse a String
Strrev() function is used to reverse a string
See below Example
<?php echo strrev("I love PHP"); // The outputs will be”PHP evol I” ?>
Function to look For a certain Text inside a String
PHP strpos() is used to get a certain text that is inside a certain string.
Incase a math has been found, strpos() will return the position of the 1st match and if no match is found,the function will return FALSE.
If a match is found, the function returns the character position of the first match. If no match is found, it will return FALSE.
Below examples searches for word “love”
<?php echo strpos("I love Programming in PHP", "love"); // The outputs is 1 ?>
Just liken arrays index,the first character of a string is always 0(Zero).
Function to Replace Text Inside a String
PHP str_replace() is used to replace a text with another in a string
Below example will replace “Java” with “PHP”
<?php echo str_replace("Java", "PHP", "I love Java!"); // This will output “I love PHP” ?>
See my Next Lesson on