开发者

How to find URLs in a string with PHP?

I kno开发者_如何学Pythonw 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?


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


See it here:

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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜