Grabbing the href attribute of an A element

前端 未结 10 2372
悲&欢浪女
悲&欢浪女 2020-11-21 05:06

Trying to find the links on a page.

my regex is:

/]*href=(\\\"\\\'??)([^\\\"\\\' >]*?)[^>]*>(.*)<\\/a>/
10条回答
  •  我在风中等你
    2020-11-21 06:07

    The following is working for me and returns both href and value of the anchor tag.

    preg_match_all("'\(.*?)\<\/a\>'si", $html, $match);
    if($match) {
        foreach($match[0] as $k => $e) {
            $urls[] = array(
                'anchor'    =>  $e,
                'href'      =>  $match[1][$k],
                'value'     =>  $match[2][$k]
            );
        }
    }
    

    The multidimensional array called $urls contains now associative sub-arrays that are easy to use.

提交回复
热议问题