Skip to content

Instantly share code, notes, and snippets.

@HikoQiu
Created October 17, 2014 09:38
Show Gist options
  • Save HikoQiu/b8de0a7b4b03a90dff77 to your computer and use it in GitHub Desktop.
Save HikoQiu/b8de0a7b4b03a90dff77 to your computer and use it in GitHub Desktop.
正则匹配网页链接
$rawTitle = '<meta name="description" content="hello world">';
$res = preg_match("/<meta name=\"(\w+)\" content=\"(.*?)\">/is",$rawTitle,$booktitle);
var_dump($booktitle, $res);
$str = 'foobar: 2008';
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
var_dump($matches);
function get_all_url($code){
preg_match_all('/<a\shref=[\"|\'](.*?)[\"|\'].*?>(.*?)<\/a>/i', $code, $arr); // *? 表示lazy模式(勉强模式)匹配*
return $arr;
}
$linkStr = '<a href="aa.com">hello</a><a href="bbb.com">bbb</a>';
$linkStr = file_get_contents('http://huxiu.com');
var_dump(get_all_url($linkStr));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment