0

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'?

3
  • Why you change your tag? Commented Feb 15, 2012 at 7:31
  • Why you no read question and see it says "PHP"? Commented Feb 15, 2012 at 7:32
  • @Kushan Sorry, I realized I put the wrong tag after I posted it. Too much of Android and PHP at the same time. Commented Feb 15, 2012 at 7:48

6 Answers 6

4

In PHP you can do :

$string = '[email protected]';
$res = explode('@', $string);
echo $res[0];

Or you can use regexp, string functions in php ... etc

Sign up to request clarification or add additional context in comments.

2 Comments

hello, how can I convert the res into a string?
you can assign the first array element to a variable: $name = $res[0]; echo $name;
3

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

Comments

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

you can try this to get string before @

Comments

0

Try This

$str1 = "Hello World";

echo trim($str1,"World");

Comments

-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

1 Comment

Was this question originally tagged with "java" or something? cause few of the answers so far have anything to do with PHP...
-1
String s = "[email protected]";
String name = s.substring(0,s.indexOf("@");

1 Comment

Sorry, it was my mistake. I changed the tags! But this is useful as well! Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.