0

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 3

2

Using a regex should take care of that. This basically works for Twitter

$text=$a_twitter_message;
preg_match_all("/http:\/\/(.*?)\/? /", $text, $link_match);
var_dump($link_match);
Sign up to request clarification or add additional context in comments.

Comments

0

See it here:

http://saturnboy.com/2010/02/parsing-twitter-with-regexp/

1 Comment

@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 equivalent
0

The 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!

http://php.net/manual/en/function.strpos.php

2 Comments

And how to extract URLs then?
I think I misunderstood your post..it said 'spot', not extract..But I guess extract makes more sense ;)

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.