PHP使用preg_replace替换curl响应中多次出现的域

PHP使用preg_replace替换curl响应中多次出现的域,php,regex,preg-replace,Php,Regex,Preg Replace,如何在PHP中使用preg_replace使用正则表达式更改href标记中出现的多个域 我只需要链接的相对路径。我的代码删除了所有内容,包括url路径和查询参数 当前链接外观 <a href="https://www.website.com/LUGAD-Clothing-Jewelry-Shoulder-Brushed/dp/B07D1V99MF/ref=sr_1_3/131-4937141-2376367/s=apparel&ie=UTF8&qid=1531422091&

如何在PHP中使用preg_replace使用正则表达式更改href标记中出现的多个域

我只需要链接的相对路径。我的代码删除了所有内容,包括url路径和查询参数

当前链接外观

<a href="https://www.website.com/LUGAD-Clothing-Jewelry-Shoulder-Brushed/dp/B07D1V99MF/ref=sr_1_3/131-4937141-2376367/s=apparel&ie=UTF8&qid=1531422091&sr=1-3&nodeID=7141123011&psd=1&keywords=clothing%2Cshoes+and+jewelry">The Link</a>

期望链接外观

<a href="/LUGAD-Clothing-Jewelry-Shoulder-Brushed/dp/B07D1V99MF/ref=sr_1_3/131-4937141-2376367/s=apparel&ie=UTF8&qid=1531422091&sr=1-3&nodeID=7141123011&psd=1&keywords=clothing%2Cshoes+and+jewelry">The Link</a>

我试过这个

$html = $this->curl->getContent($completeUrl);

$newhtml = preg_replace('/<a(.*)href="([^"]*)"(.*)>/','<a$1href="/"$3>',$html);
$html=$this->curl->getContent($completeUrl);

$newhtml=preg_replace('/根据您的问题猜测,您应该使用如下正则表达式:

(<a\s+href\s*=\s*")(?:https?:\/\/)?www\.website\.com\/

使用str_replace不是更合适吗?-
$html=str_replace('https://www.website.com/“,”,$html);
-避免在您的regexp中使用
*
,它很贪婪,并且会跨多个锚匹配。尝试了这两种方法,它们都有效。因为在href标记之前我有其他属性,所以我选择了第二个。它很有魅力。非常感谢。
preg_replace('/(<a\s+href\s*=\s*")(?:https?:\/\/)?www\.website\.com\//i', '$1', $str);
(<a(?:(?!href).)* href\s*=\s*")(?:https?:\/\/)?www\.website\.com\/