Finding URL & Email text in a string? |
[eluser]carllawll[/eluser]
I've always been absolutely terrible with regex, it's just not my thing! ![]() Would anyone be able to give us a hand finding out if a string contains a url or email examples: [email protected] http://www.a.a http://a.a www.a.a How difficult would something like this be?
[eluser]dejan[/eluser]
Do you need to extract an email and/or URL from a string, or check if a string is valid email/url? If latter, it's easy: filter_var($string, FILTER_VALIDATE_EMAIL); filter_var($string, FILTER_VALIDATE_URL); If former, it's a bit trickier. You can either try with regex, which I suggest you google for, or you can tokenize the string and check each token, like so: foreach (preg_split('/\b/', $string) as $token) { if (filter_var($token, FILTER_VALIDATE_EMAIL)) return $token; } \b in regex is word boundry (space, comma, dot, semicolon, etc.).
[eluser]carllawll[/eluser]
It's not going to just be an email or url, I want to know if it contains either as I don't want users to be able to contain either in posts they submit!
[eluser]dejan[/eluser]
Why not use the second solution I provided? E.g. this piece of code: foreach (preg_split(’/\b/’, $string) as $token) { if (filter_var($token, FILTER_VALIDATE_EMAIL)) return $token; }
[eluser]DarkManX[/eluser]
http://en.wikipedia.org/wiki/Regular_expression use preg_match_all() to get the matches. |
Welcome Guest, Not a member yet? Register Sign In |