Get userid and mail server from email using PHP string functions

PHP string functions explode() and strstr() are very useful functions when working on strings. While explode() converts a string into array using a separator, strstr() searches for a string in another string.

explode()

Using explode() 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, first element is "test_email" (userid) and second element is "gmail.com" (server name). So, using this function you can easily get the email userid and the mail server name form 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 that particular 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 below screen;

explode and strstr functions in php

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 third parameter, it returns the string before the first occurrence of '@'. So we get the userid. If we omit the third parameter, then it gives the string from the first occurrence of '@'. Then we used substr() function to get rid of '@'. 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, you will see below output:

explode and strstr functions in php

You can see both the outputs are same.

explode and strstr functions in phpConclusion

This was an example of using string functions in PHP. There are many useful string functions available in PHP, you can use them while working in PHP.