![]() |
Finding URL & Email text in a string? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Finding URL & Email text in a string? (/showthread.php?tid=53966) |
Finding URL & Email text in a string? - El Forum - 08-16-2012 [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? Finding URL & Email text in a string? - El Forum - 08-16-2012 [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.). Finding URL & Email text in a string? - El Forum - 08-16-2012 [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! Finding URL & Email text in a string? - El Forum - 08-17-2012 [eluser]carllawll[/eluser] Are there no regex wizards that can help me? ![]() Finding URL & Email text in a string? - El Forum - 08-17-2012 [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; } Finding URL & Email text in a string? - El Forum - 08-17-2012 [eluser]DarkManX[/eluser] http://en.wikipedia.org/wiki/Regular_expression use preg_match_all() to get the matches. |