简体   繁体   中英

php regex to get string inside href tag

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:

<a href="theurltoget.com">URL</a>

Additionally, I only want the base url part. Ie from http://www.mydomain.com/page.html i only want http://www.mydomain.com/

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

this expression will handle 3 options:

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

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

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"] $href = $info["scheme"] . "://" . $info["host"] $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"]; $href = $info["scheme"] . "://" . $info["host"] . $info["path"]; Which gives you:

// http://www.mydomain.com/page.html

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

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;

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.

Because Positive and Negative Lookbehind are cool

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

It will match only what you want, without quotation marks

Array ( [0] => theurltoget.com )

/href="(https?://[^/]*)/

我认为您应该能够处理其余的工作。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM