Operation on Strings using pre-defined string functions in PHP
In this PHP tutorial, we are going to perform different operations on strings using pre-defined string functions in PHP.
Some of the pre-defined string functions in PHP:-
1.) strlen() :- strlen() is used to get the length of the string in PHP . A string whose length is to find is passed as a parameter to strlen() function. It returns the length of the string.
Code Snippet for strlen()
<?php echo strlen("String"); ?>
It will return the length of string 6.
2.) str_word_count :- str_word_count() function counts the number of words in a string . String in Which the total number of words is to count is passed as a parameter to str_word_count function. It will return an integer value representing total number of words in strings .
Code Snippet for str_word_count()
<?php echo str_word_count("This is an example of string"); ?>
It will return the total number of words i.e 6.
3.) strrev() :- strrev() function reverses a string . A string which has to be reversed is passed as a parameter to strrev() function. It returns a string reverse of the original string.
Code Snippet for strrev()
<?php echo strrev("String"); ?>
It will return gnirtS which are the reverse of the original string.
4.) strpos() :- strpos() function searches for a specific text within a string . String along with text to be found is passed as parameter separated by ‘,’
If a match is found, the function returns the character position of the first match. If no match is found it will return false.
Code Snippet for strpos()
<?php echo strpos("This is a string","is"); ?>
It will return 6 i.e. character position of the first match of text “is”.
5.) str_replace() :- str_replace() function replaces some characters with some other characters in a string . Character to be changed, replacing the character, and original string is passed as parameters to str_replace() function respectively.
Code Snippet for str_replace()
<? echo str_replace("world" , "PHP" , "Hello World "); ?>
It will return Hello PHP replacing “world” with “PHP”.
Also, learn
PHP Code to perform different operation on Strings
<?php echo "1.)Counting length of String\n"; echo "Length of String - \"Codespeedy\" is :"; echo strlen("Codespeedy"); echo "\n\n2.)Counting number of words in String\n"; echo "No. of words in string -\"Codespeedy is a programming website\" is :"; echo strlen("Codespeedy"); echo "\n\n3.)Reverse of String\n"; echo "Reverse of String - \"Codespeedy\" is :"; echo strrev("Codespeedy"); echo "\n\n4.)Search for a Specific Text in a String\n"; echo "Position of Word \"Tutorial\" in String if found -\"Codespeedy is a programming tutorial website\" is :"; echo strpos("Codespeedy is a programming tutorial website","tutorial"); echo "\n\n5.)Replace a specific text within a String with another text\n"; echo "Replacing \"World\" with \"PHP\" in \"Hello World\" :- "; echo str_replace("World" , "PHP" , "Hello World "); ?>
OUTPUT
1.)Counting length of String Length of String - "Codespeedy" is :10 2.)Counting number of words in String No. of words in string -"Codespeedy is a programming website" is :10 3.)Reverse of String Reverse of String - "Codespeedy" is :ydeepsedoC 4.)Search for a Specific Text in a String Position of Word "Tutorial" in String if found -"Codespeedy is a programming tutorial website" is :28 5.)Replace a specific text within a String with another text Replacing "World" with "PHP" in "Hello World" :- Hello PHP
Leave a Reply