seriousdev
seriousdev

Reputation: 7656

How to find URLs in a string with PHP?

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?

Upvotes: 0

Views: 121

Answers (3)

Oli
Oli

Reputation: 2452

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

Upvotes: 0

Mark Snidovich
Mark Snidovich

Reputation: 1055

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);

Upvotes: 2

Related Questions