PHP string functions explode()
and strstr()
are useful when working on strings. While explode() converts a string into an array using a separator, strstr() searches for a string in another string.
explode()
Using explode()
PHP function you can create an array of values separated by a specific character from a string. if the string is "test_email@gmail.com", using '@' as the separator we can create an array as below:
<?php
$email = "test_email@gmail.com";
$arr = explode('@',$email); // creates an array of strings separated by '@'
$user = $arr[0];
$server = $arr[1];
Here, $arr is an array of two elements, the first element is "test_email" (userid) and the second element is "gmail.com" (server name). So, using this function you can easily get the mail userid and the mail server name from an email id.
strstr()
We can do the same thing using strstr()
or stristr()
(case-insensitive) function. Using strstr()
you can search a particular character/string in another string and get the values before or after the searched character/string. See below:
<?php
$userid = strstr($email,"@",true); // searches '@' and returns string before first occurrence of '@'
$server = strstr($email,"@"); // searches '@' and returns string from first occurrence of '@'
$server = substr($server,1); // remove '@'
We will write two simple programs to display user id and mail server name from an email id.
Using explode() function
explode.php
<html>
<head>
<title>Find userid and server name from Email id</title>
</head>
<body style="text-align: center;">
<h1>Find userid and server name from Email id using explode()</h1>
<?php
$email = "test_email@gmail.com";
$arr = explode('@',$email); // creates an array strings separated by '@'
echo "Email id :<b>".$email."</b><br><br>";
echo "Userid: <b>".$arr[0]."</b><br>";
echo "Server: <b>".$arr[1]."</b>";
?>
</body>
</html>
Using '@' as the separator, an array is created with 2 elements. Let us name this file as explode.php
and save it in a folder named "php" under xampp/htdocs.
Run localhost/php/explode.php. You will see the below screen;
Using strstr() function
strstr.php
<html>
<head>
<title>Find userid and Server from Email id</title>
</head>
<body style="text-align: center;">
<h1>Find userid and server name from Email id using strstr()</h1>
<?php
$email = "test_email@gmail.com";
$userid = strstr($email,"@",true); // searches '@' and returns string before first occurrence of '@'
$server = strstr($email,"@"); // searches '@' and returns string from first occurrence of '@'
$server = substr($server,1); // remove '@'
echo "Email id :<b>".$email."</b><br><br>";
echo "Userid: <b>".$userid."</b><br>";
echo "Server: <b>".$server."</b>";
?>
</body>
</html>
Here, we are using '@' as the search character. When we use 'true' as the third parameter, it returns the string before the first occurrence of '@'. So, we get the userid. If we omit the third parameter, it gives the string from the first occurrence of '@'. Then we used substr()
function to remove '@'. So, this gives the server name. Let us name this file as strstr.php
and save it in a folder "php" under xampp/htdocs.
Run localhost/php/strstr.php and you will see the below output:
You can see that both the outputs are the same.
Post a Comment