Hi Im attempting to search a string to see whether it contains a email address - and then return it.
A typical email vaildator expression is:
eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
However how would I search if that is in a string, for example return the email address in the string:
"Hi my name is Joe, I can be contacted at [email protected]. I am also on Twitter."
I am a bit stumped, I know I can search if it exists at all with \b around it but how do I return what is found.
Thanks.
You could use preg_match()
, which would output it to an array for use.
$content = "Hi my name is Joe, I can be contacted at [email protected]. I am also on Twitter.";
preg_match("/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/i", $content, $matches);
print $matches[0]; // [email protected]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With