$haystack="myuserid@example.com";
$needle="@";
echo strstr($haystack,$needle,false);// @example.com
Output is
@example.com
Syntax
echo strstr($haystack,$needle,$before);
$haystack
$needle
$before
$needle
( excluding the $needle ) $haystack="myuserid@example.com";
$needle="@";
echo strstr($haystack,$needle,true);// myuserid
Output is
myuserid
$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
$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.
$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
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.