Php 使用正则表达式来设置HREF属性的目标

Php 使用正则表达式来设置HREF属性的目标,php,regex,Php,Regex,我对regex很陌生 我想针对href=“”中引号之间的所有内容,以便快速解析html并替换链接引用的内容 我还希望能够使用imgsrc属性来实现这一点,但如果有人能够解释如何使用href实现这一点,我将能够以同样的方式实现其他属性 如果我有这个标记: <a href="http://my.domain/simple-product-2.html" class="product-image"><img src="http://my.domain/media/catalog/pr

我对regex很陌生

我想针对
href=“”
中引号之间的所有内容,以便快速解析html并替换链接引用的内容

我还希望能够使用
img
src
属性来实现这一点,但如果有人能够解释如何使用
href
实现这一点,我将能够以同样的方式实现其他属性

如果我有这个标记:

<a href="http://my.domain/simple-product-2.html" class="product-image"><img src="http://my.domain/media/catalog/product/cache/1/small_image/75x/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg" width="75" height="75" alt="Simple Product 2" title="Simple Product 2"></a>
<div class="product-details">
    <h3 class="product-name"><a href="http://my.domain/simple-product-2.html">Simple Product 2</a></h3>
    <div class="price-box">
        <span class="regular-price" id="product-price-2-related">
        <span class="price">$42.00</span>                                    </span>
    </div>
    <p><a href="http://my.domain/wishlist/index/add/product/2/form_key/PLOSE4N7mH4kcOgX/" class="link-wishlist">Add to Wishlist</a></p>
</div>
检索此输出:

href="http://index.html"
我想针对href=“”中引号之间的所有内容

按照下面注释中@lcoderre的建议,从索引1中获取匹配的组

href="([^"]*+)"
这是


也可以试试这个

我想针对href=“”中引号之间的所有内容

按照下面注释中@lcoderre的建议,从索引1中获取匹配的组

href="([^"]*+)"
这是


也可以试试这个


不要使用正则表达式解析HTML。使用:

$doc=newDOMDocument();
libxml\u使用\u内部错误(true);
$doc->loadHTML($html);//加载你的html
$nodelist=$doc->getElementsByTagName('a');//拿到所有的标签
对于($i=0;$i<$nodelist->length;$i++){
$node=$nodelist->item($i);
$val=$node->attributes->getNamedItem('href')->nodeValue;
echo“href为:$val\n”;
}

不要使用正则表达式解析HTML。使用:

$doc=newDOMDocument();
libxml\u使用\u内部错误(true);
$doc->loadHTML($html);//加载你的html
$nodelist=$doc->getElementsByTagName('a');//拿到所有的标签
对于($i=0;$i<$nodelist->length;$i++){
$node=$nodelist->item($i);
$val=$node->attributes->getNamedItem('href')->nodeValue;
echo“href为:$val\n”;
}

预期输出是什么?请分享。从这里开始阅读。自己学习一些正则表达式;t.output不是有效的url。预期的输出是什么?请分享。从这里开始阅读。自己学习一些正则表达式;t hurt.output不是一个有效的url。根据记录,使用所有格量词()可能会加快速度:
href=“([^”]*+)”
是的,你是对的。感谢分享知识。我也会在我的帖子中更新它。根据记录,使用所有格量词()可能会加快速度:
href=“([^”]*+)”
是的,你是对的。谢谢分享知识。我也会在我的帖子中更新它。
(?<=href=").*?(?=")
$re = "/href=\\"([^\\"]*+)\\"/m";
$str = ...

preg_match_all($re, $str, $matches);
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML( $html ); // loads your html

$nodelist = $doc->getElementsByTagName('a'); // get all the <a> tags
for($i=0; $i < $nodelist->length; $i++) {
    $node = $nodelist->item($i);
    $val = $node->attributes->getNamedItem('href')->nodeValue;
    echo "href is: $val\n";
}