我被一个刚刚得到帮助的问题困住了——这是一个新问题,但只是轻微的。

我有这个 preg_match 来获取 href 的内容。请不要告诉我不要使用正则表达式 - 我知道使用其他解析器/类等,但这是一个旧脚本,现在只需要修复。:) 没有时间重写!

preg_match("~<a target=\'_blank\' rel=\'nofollow\' href=\"(.*?)\">~i", $epilink, $epiurl);

它返回:

http://www.example.com/frame2.php?view=&epi=54673-r

但是,它应该返回:

http://www.example.com/frame2.php?view=168204&epi=54673

这是一个可以使用的 html 示例:

<a target='_blank' rel='nofollow' href="http://www.example.com/frame2.php?view=545903&epi=54683">

为什么我返回的 URL 格式错误?

感谢大家的帮助。

有帮助吗?

解决方案

$string="<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
$s = explode('">',$string);
foreach($s as $k){
   if (strpos($k,"href")!==FALSE){
        echo preg_replace('/.*href="|/ms',"",$k);
        break;
   }
}

输出

$ php test.php
http://www.example.com/frame2.php?view=545903&epi=54683

其他提示

这应该有效:

$epilink = "<a target='_blank' rel='nofollow' href=\"http://www.example.com/frame2.php?view=545903&epi=54683\">";
preg_match("/<a target='_blank' rel='nofollow' href=\"(.*?)\">/i", $epilink, $epiurl);

print_r($epiurl);

你也可以使用 预匹配全部

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top