How to get a substring from a string through PHP?

Hi, I want to change the display username, for example abcd@somedomain.com , only to abcd. so for this I have to pin the part starting with @.

I can do this very easily through the variablename.substring () function in Java or C #, but I don't know with the PHP syntax. So help me do this.

Suppose that I m ​​has a type variable.

$username = " abcd@somedomain.com "; $username = some 

the string manipulation function should be called here; so echo $ username; can only lead to abcd.

+4
source share
5 answers

Try the following:

 $username = substr($username, 0, strpos($username, '@')); 
+9
source

Use strtok() .

 $username = strtok($email, '@'); 

CodePad

+4
source

Use strstr .

An example from a PHP link is

 <?php $email = ' name@example.com '; $domain = strstr($email, '@'); echo $domain; // prints @example.com $user = strstr($email, '@', true); // As of PHP 5.3.0 echo $user; // prints name ?> 
+3
source
 list($username, $domain) = explode('@', ' asdf@somedomain.com ') 
+3
source
 substr(string, 0, 20) 

String, start, length

+1
source

All Articles