Reputation: 183
I am trying to trim a string in PHP so that I can only get certain text from the String.
I have an email stored to a String for instance [email protected] .
How can I remove the text after the '@' so that I would only 'some_name'?
Upvotes: 0
Views: 2913
Reputation: 23542
You should know both ways to do this:
$mail = "[email protected]";
echo substr($mail, 0, strpos($mail, '@') );
list($name, $domain) = explode('@', $mail);
echo $name;
If you don't need the $domain you can skip it:
list($name) = explode('@', $mail);
More about list.
Demo: http://ideone.com/lbvQF
Upvotes: 3
Reputation: 590
In PHP you can do :
$string = '[email protected]';
$res = explode('@', $string);
echo $res[0];
Or you can use regexp, string functions in php ... etc
Upvotes: 4
Reputation: 4631
$str = '[email protected]';
$strpos = strpos($str, "@");
echo $email = substr($str, 0,$strpos);
you can try this to get string before @
Upvotes: 2
Reputation: 1555
String s = "[email protected]";
String name = s.substring(0,s.indexOf("@");
Upvotes: -1
Reputation: 1
You could try split using regex and the @ symbol. This will return two Strings which you can then use just to acquire the 'some_name'.
http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/String.html
Upvotes: -1