[英]php regex to get string inside href tag
我需要一个正则表达式,可以在href标签和引号内为我提供字符串。
例如,我需要在以下位置提取theurltoget.com:
<a href="theurltoget.com">URL</a>
另外,我只想要基本网址部分。 即来自http://www.mydomain.com/page.html
我只希望http://www.mydomain.com/
不要为此使用正则表达式。 您可以使用xpath和内置的php函数来获取所需的内容:
$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
此表达式将处理3个选项:
'/ href = [“ \\']?([^^ \\'>] +)[” \\']?/'
如果您只在寻找基本网址部分 (@David问题的第二部分),请使用@Alec的答案!
$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]);
这将为您提供:
$info
Array
(
[scheme] => http
[host] => www.mydomain.com
[path] => /page.html" class="myclass" rel="myrel
)
因此,您可以使用$href = $info["scheme"] . "://" . $info["host"]
$href = $info["scheme"] . "://" . $info["host"]
$href = $info["scheme"] . "://" . $info["host"]
可为您提供:
// http://www.mydomain.com
当您在href之间查找整个URL时 ,您应该使用另一个正则表达式,例如@ 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]);
这将为您提供:
$info
Array
(
[scheme] => http
[host] => www.mydomain.com
[path] => /page.html
)
现在,您可以使用$href = $info["scheme"] . "://" . $info["host"] . $info["path"];
$href = $info["scheme"] . "://" . $info["host"] . $info["path"];
这给你:
// http://www.mydomain.com/page.html
http://www.the-art-of-web.com/php/parse-links/
让我们从最简单的情况开始-格式正确的链接,没有额外的属性:
/<a href=\"([^\"]*)\">(.*)<\/a>/iU
对于所有href值替换:
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;
因为正向和负向落后很酷
/(?<=href=\").+(?=\")/
它只会匹配您想要的内容,不带引号
数组([0] => theurltoget.com)
/href="(https?://[^/]*)/
我认为您应该能够处理其余的工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.