Searching & Replacing String
Write any some logical PHP program you need to search some text for a particular chunk of string. Like, need to know the domain name of user email id or want to know the current page name from page URL.
In PHP language there are some pre-define function to search particular string from any text.
- strpos() and strrpos()
- strstr()
- substr_count()
- printf()
- sprintf()
- fprintf()
- vprintf()
i. strpos() and strrpos()
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <!--?php echo strpos($emailid, 'gmail '); echo '<BR--> '; echo strpos($emailid, 'yahoo '); echo ' '; if(strpos($emailid, ' gmail ') !== false){ echo 'Email id right '; } else{ echo 'Wrong Email id '; } echo ' '; echo strpos($emailid, '. '); echo ' '; echo strrpos($emailid, '. '); ?> |
Output
1 2 3 4 5 | 11 -1 Email id right 4 16 |
Explanation
strpos() function return the position** of any string value from whole text. The first argument is text value and the second argument is string (which you want to find) value. If the string is found then returns the position of this string and if not found it returns -1.
In above example, we want to search the gmail position from [email protected] It returns 11 the position value of gmail word.
But when we search yahoo it returns -1 because yahoo is not present in [email protected]
strrpos() is similar to strpos() but very minor difference.
strpos() find the string from the starting position and strrpos() find the string from the ending position of text.
So above example strpos() return the first dot(.) position and strrpos() return last dot(.) position
Note: ** String position start with zero. Be careful when you using strpos() to search the first string position in any text its returns zero which equivalent to false in PHP (if you check the code in if condition). That’s why use === or !== operator to avoid those problem.
ii strstr()
1 2 3 4 5 6 7 8 9 10 11 | <!--?php echo strstr($emailid, 'gmail '); echo '<BR--> '; if(strstr($emailid, ' yahoo')){ echo 'Found'; } else{ echo 'Not Found'; } ?> |
Output
1 2 | gmail.com Not Found |
Explanation
strstr() function just check the string is present or not in the text. If present returns the string value along with reaming text and if not present return false.
So in above example first gmail is found so return gmail.com
And the second yahoo not found so return Not Found.
Note: strstr() is case sensitive means if you search Gmail it returns false. If you don’t care about the matching case then use stristr() which is case insensitive.
iii. substr_count()
1 2 3 4 |
Output
1 | 2 |
Explanation
substr_count() function used to find the number of times of a text appears in the target string.
In above example, o is present two times so it returns 2.
In your PHP program sometimes you need to replace wrong text from any string. Like spelling check, Wrong spelling replaces to correct spelling. In this case PHP gives you str_replace() function,
Example
1 2 3 4 | <!--?php $str = "using striing replace function to correct striing spelling"; echo str_replace('striing', 'string',$str); ?--> |
Output
1 | using string replace function to correct string spelling |
Explanation
str_replace() function used to replace any striing to another string in the chunk of text. The first argument is which striing you want to replace, the second argument is which string replace the previous string and the third argument is where the to replace is actually happening means the text value.
In above example, you try to replace the wrong striing spelling to correct spelling. So pass the first argument wrong spelling ‘striing’, the second pass the correct spelling ‘string’ and third the text value ‘using string replace function to correct string spelling’.
After execution, all ‘striing’ word replace to ‘string’.
Note: str_replace() is case sensitive means if you want to replace String it returns false. If you don’t care about the matching case then use str_ireplace() which is case insensitive.
Formatting String
PHP allows to format strings in many different ways to make easy for people to read or passing to another program.
There are many PHP function to format strings in specific ways.
1. printf()
1 2 3 4 | <!--?php printf("String format %s as a integer ( %d), a float (%f), binary integer(%b)",123.45,123.45,123.45,123.45); ?--> |
Output
1 2 | String format 123.45 as a integer ( 123), a float (123.450000), binary integer(1111011) |
Explanation
In printf() the first argument is a string, it is called format string. It contains regular text or some format specifiers (like in above example %s). format specifiers start with % symbol.
The second argument is the value which you want to format particular way.
In above example 123.45 format into co-responding its float, integer and binary integer value style.
There are some symbol of different format specifier, like %s for string, %f for float, %d for decimal integer, %b for binary integer, %c for ASCII, %e for scientific notation, %x for hexadecimal integer and %o for octal integer.
2. sprintf()
sprintf() is similar to prinf() except that rather than directly outputting the result it returns, it so that you can store it in a variable. This is help to process the result before displaying it or store into database also.
1. printf()
1 2 3 4 | <!--?php $var = sprintf("String format %s as a integer ( %d), a float (%f), binary integer(%b)",123.45,123.45,123.45,123.45); echo $var; ?--> |
3. fprintf() use for writing the result to a file.
iv.4. vprintf() use for work with the array of values to format instead of an argument list.