php regex to get string inside href tag

后端 未结 9 2129
忘了有多久
忘了有多久 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:37

    For all href values replacement:

    function replaceHref($html, $replaceStr)
    {
        $match = array();
        $url   = preg_match_all('/<a [^>]*href="(.+)"/', $html, $match);
    
        if(count($match))
        {
            for($j=0; $j<count($match); $j++)
            {
                $html = str_replace($match[1][$j], $replaceStr.urlencode($match[1][$j]), $html);
            }
        }
        return $html;
    }
    $replaceStr  = "http://affilate.domain.com?cam=1&url=";
    $replaceHtml = replaceHref($html, $replaceStr);
    
    echo $replaceHtml;
    
    0 讨论(0)
  • 2020-11-30 10:37

    Because Positive and Negative Lookbehind are cool

    /(?<=href=\").+(?=\")/
    

    It will match only what you want, without quotation marks

    Array ( [0] => theurltoget.com )

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

    http://www.the-art-of-web.com/php/parse-links/

    Let's start with the simplest case - a well formatted link with no extra attributes:

    /<a href=\"([^\"]*)\">(.*)<\/a>/iU
    
    0 讨论(0)
  • 2020-11-30 10:49

    Use the answer by @Alec if you're only looking for the base url part (the 2nd part of the question by @David)!

    $html = '<a href="http://www.mydomain.com/page.html" class="myclass" rel="myrel">URL</a>';
    $url = preg_match('/<a href="(.+)">/', $html, $match);
    $info = parse_url($match[1]);
    

    This will give you:

    $info
    Array
    (
        [scheme] => http
        [host] => www.mydomain.com
        [path] => /page.html" class="myclass" rel="myrel
    )
    

    So you can use $href = $info["scheme"] . "://" . $info["host"] Which gives you:

    // http://www.mydomain.com  
    

    When you are looking for the entire url between the href, You should be using another regex, for instance the regex provided by @user2520237.

    $html = '<a href="http://www.mydomain.com/page.html" class="myclass" rel="myrel">URL</a>';
    $url = preg_match('/href=["\']?([^"\'>]+)["\']?/', $html, $match);
    $info = parse_url($match[1]);
    

    this will give you:

    $info
    Array
    (
        [scheme] => http
        [host] => www.mydomain.com
        [path] => /page.html
    )
    

    Now you can use $href = $info["scheme"] . "://" . $info["host"] . $info["path"]; Which gives you:

    // http://www.mydomain.com/page.html
    
    0 讨论(0)
  • 2020-11-30 10:49
    /href="(https?://[^/]*)/
    

    I think you should be able to handle the rest.

    0 讨论(0)
  • 2020-11-30 10:51
    $html = '<a href="http://www.mydomain.com/page.html">URL</a>';
    
    $url = preg_match('/<a href="(.+)">/', $html, $match);
    
    $info = parse_url($match[1]);
    
    echo $info['scheme'].'://'.$info['host']; // http://www.mydomain.com
    0 讨论(0)
提交回复
热议问题