我只想获取没有任何参数的 url 的"干净"版本。低。。。如果URL内有一个问号,请将其删除,然后删除所有内容。
这是我目前的行:
preg_match_all('/<a(.*?)href=("|''|)(.*?)("|''| )(.*?)>/s',$content,$ahref);
只是为了在这里更清楚...我希望这个网址(例如):
/go/page/mobile_download_apps.html?&who=r,6GDewh28SCW3/fUSqmWqR_E9ljkcH1DheIMqgbiHjlX3OBDbskcuCZ22iDvk0zeZR7BEthcEaXGFWaQ4Burmd4eKuhMpqojjDE6BrCiUtLClkT32CejpMIdnqVOUmWBD
将:
/go/page/mobile_download_apps.html
with DOMDocument, strpos, substr:
$dom = new DOMDocument;
$dom->loadHTML($content);
$linkNodeList = $dom->getElementsByTagName('a');
foreach($linkNodeList as $linkNode) {
$href = $linkNode->getAttribute('href');
if ( false !== ($offset = strpos($href, '?')) )
$linkNode->setAttribute('href', substr($href, 0, $offset));
}
$newContent = $dom->saveHTML();
或带爆炸:
$linkNode->setAttribute('href', explode('?', $href)[0]);
你的意思是这种行为:
<a's+href's*='s*"'K[^"?]+
$result = preg_replace('/<a's+href's*='s*"'K[^"?]+/im', '', $text);
正如评论中提到的,你不应该使用正则表达式获取标签,你应该使用解析器。不过,你来了:
<a[^>]+href=("|')([^"'?]*)[^"']*'1[^>]*>
演示:https://regex101.com/r/tV5pP8/3
Opps...我这边注意力不集中:)
自己解决了...(超级简单)
这是最后一行:
preg_match_all('/<a(.*?)href=("|''|)(.*?)('?|"|''| )(.*?)>/s',$content,$ahref);