替换所有<a> and <link/> elements</a>的href值

时间:2014-11-14 01:17:28

标签: php regex

我试图编写两个不同的函数来替换href属性的值。因此,第一个函数应该替换锚元素的href值,而第二个函数应该替换href。需要两个功能,因为我想做不同的事情。因此,例如对于<a> href,我只想准备一些url,对于<link> href,我想根据某些逻辑添加不同的url。我从这开始:

$pattern = "/(?<=href=(\"|'))[^\"']+(?=(\"|'))/";
$response = preg_replace_callback($pattern, 'html_href',  $response);

这不是我想要的,因为html_href函数不知道href的值来自哪个,以及哪个值是前置的。如果有人可以帮助我编写正则表达式模式,那么这对于这种类型的调用会更好:

$pattern_anchor = ""; // ??
$response = preg_replace_callback($pattern, 'anchor_href',  $response);

$pattern_link = ""; // ?
$response = preg_replace_callback($pattern, 'link_href',  $response);

这些函数只是获得href值。我对正则表达式不太满意,感谢所有的帮助,谢谢!

0 个答案:

没有答案
相关问题