Searching for a word inside a text string

echo strstr($haystack,$needle,$before);
$haystack
Required: Input string ( main string for searching )
$needle
Required: used to search the first occurance inside Input string
$before
Optional: default FALSE, if set to TRUE then output returns the string before the first occurrence of $needle ( excluding the $needle )
Output is the part of string after the needle ( if $before is FALSE ) or before the needle. FALSE is returned if matching is not found.

This is case sensitive search. Use stristr() for case in-sensitive search

Example 1 of strstr()

$haystack="myuserid@example.com";
$needle="@";
echo strstr($haystack,$needle,false);// @example.com
Output is
@example.com

Example 2 of strstr()

$haystack="myuserid@example.com";
$needle="@";
echo strstr($haystack,$needle,true);// myuserid
Output is
myuserid

Example 3 of strstr()

$haystack="Welcome to plus2net.com to learn PHP";
$needle="to";
echo strstr($haystack,$needle);// to plus2net.com to learn PHP
Output is
to plus2net.com to learn PHP

Uses of strstr

We can find out existence of a word or combination of words in a string. For example we don't want some words to be posted by the user while submitting any message or text to the system. We can check the existence of the word by using stristr() function and know the presence of the string. This is case insensitive checking.

For a case sensitive checking we can use strstr function

Another example is checking of links posted by users in a site where any one can post jokes. Some people were posting links in the text area to their sites. To prevent that , stristr function is used to check the presence of https:// in the posted message. Or it can be checked the presence of <a in the posted string to identify a link. Here is the syntax of this function.

string stristr (main string , checking string)

This function returns true if the checked string is found within the main string. Now here is a code which uses stristr function and returns true after checking the word.
$main_string="This is a test string <a href=https://www.plus2net.com>with a link</a> to plus2net";
if(stristr($main_string,"https://")){
echo "String contains a link or the text https:// inside it ";}
else {
echo "String does not contains a link or the text https:// inside it ";}
Let us try to collect domain part from the email address by using strstr function.

$email = "myname@mysitenamehere.com";
$domain = strstr ($email, "@");
echo $domain; // prints @mysitenamehere.com
We can also use split command to get the domain part from email address.

Searching Blank space inside a string

$string='first second';
if (strstr($string,' ')) {
echo " There is blank space present inside the string ";
}else{
echo " There is NO blank space present inside the string ";
}
The output is
 There is blank space present inside the string
Visitors Rating
Your Rating





PHP String Functions

Post your comments , suggestion , error , requirements etc here .




HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
©2000-2019 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer