php regex to get string inside href tag

后端 未结 9 2130
忘了有多久
忘了有多久 2020-11-30 10:11

I need a regex that will give me the string inside an href tag and inside the quotes also.

For example i need to extract theurltoget.com in the following:

         


        
相关标签:
9条回答
  • 2020-11-30 10:52

    This will handle the case where there are no quotes around the URL.

    /<a [^>]*href="?([^">]+)"?>/
    

    But seriously, do not parse HTML with regex. Use DOM or a proper parsing library.

    0 讨论(0)
  • 2020-11-30 10:57

    Dont use regex for this. You can use xpath and built in php functions to get what you want:

        $xml = simplexml_load_string($myHtml);
        $list = $xml->xpath("//@href");
    
        $preparedUrls = array();
        foreach($list as $item) {
            $item = parse_url($item);
            $preparedUrls[] = $item['scheme'] . '://' .  $item['host'] . '/';
        }
        print_r($preparedUrls);
    
    0 讨论(0)
  • 2020-11-30 11:03

    this expression will handle 3 options:

    1. no quotes
    2. double quotes
    3. single quotes

    '/href=["\']?([^"\'>]+)["\']?/'

    0 讨论(0)
提交回复
热议问题