DroidMatt
DroidMatt

Reputation: 183

Removing Certain Text from String in PHP

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

Answers (6)

Labhansh Satpute
Labhansh Satpute

Reputation: 449

Try This

$str1 = "Hello World";

echo trim($str1,"World");

Upvotes: 0

PiTheNumber
PiTheNumber

Reputation: 23542

You should know both ways to do this:

substr

$mail = "[email protected]";
echo substr($mail, 0, strpos($mail, '@') );

explode

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

mlinuxgada
mlinuxgada

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

Poonam
Poonam

Reputation: 4631

$str = '[email protected]';
$strpos = strpos($str, "@");
echo $email = substr($str, 0,$strpos);

you can try this to get string before @

Upvotes: 2

jinreal
jinreal

Reputation: 1555

String s = "[email protected]";
String name = s.substring(0,s.indexOf("@");

Upvotes: -1

Dane
Dane

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

Related Questions