I know there is filter_var() but I don't want to validate a URL, I want to spot them in a whole text (e.g. a tweet). So you got any idea?
3 Answers
1 Comment
Matt Gibson
@sexyprout Regexps are normally delimited with /slashes/, but that can make things difficult to read if you're actually looking for slashes in the regular expression (as you are, to parse
http://www.example.com/whatever...) because you have to escape the slash characters in the expression. To make it easier, you can use any character you want to delimit your regular expression. In this case, "@" is being chosen as an arbitrary character that doesn't need to appear in the expression itself. For example, the regular expressions /http:\/\/(.*)\/(.*)/ and @http://(.*)/(.*)@ are equivalentThe regular expression solutions are fine, but here's another simple way: use strpos.
if(strpos($text, "http://") !== false) {
print "url found";
}
use stripos for case-insensitive.
Also, be aware that the other regular expression examples don't check for 'https' or just urls starting with 'www' only!
2 Comments
seriousdev
And how to extract URLs then?
Oli
I think I misunderstood your post..it said 'spot', not extract..But I guess extract makes more sense ;)