繁体   English   中英

使用PHP替换href =“”之间的特定完整链接

[英]Replace Specifc Full Links Between href=“ ” Using PHP

我尝试搜索相关答案,但找不到适合我特定需求的东西。 我的一个wordpress网站上有成千上万的文章有很多会员链接-所有链接都以相同的url格式和子域结构开头:

http://affiliateprogram.affiliates.com/

但是,在初始URL格式之后,查询字符串将针对每个单独的URL附加更改,以便将访问者发送到目标网站上的特定页面。

我正在寻找一种可以扫描包括上面特定域的所有href链接的html代码字符串(文章正文),然后用我选择的另一个标准链接替换“全链接”(无论附加查询字符串如何)。

href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination"

被替换为

href="http://www.mylink.com"

理想情况下,我希望通过php进行此操作,因为我有基本的了解,但是如果您有任何其他建议,我将不胜感激。

提前致谢。

<?php

$html = 'href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination"';

echo preg_replace('#http://affiliateprogram.affiliates.com/([^"]+)#is', 'http://www.mylink.com', $html);

?>

http://ideone.com/qaEEM

使用正则表达式,例如:

href="(https?:\/\/affiliateprogram.affiliates.com\/[^"]*)"

$data =<<<EOT
  <a href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination">bar</a>
  <a href="http://stackoverflow.com/questions/8490284/replace-specifc-full-links-between-href-using-php">foo</a>
  <a name="zz" href="http://affiliateprogram.affiliates.com/?query=random&page=destination&string">baz</a>
EOT;

echo (
  preg_replace (
    '#href="(https?://affiliateprogram.affiliates.com/[^"]*)"#i',
    'href="http://www.mylink.com"',
    $data
  )
);

输出

<a href="http://www.mylink.com">bar</a>
<a href="http://stackoverflow.com/questions/8490284/replace-specifc-full-links-between-href-using-php">foo</a>
<a name="zz" href="http://www.mylink.com">baz</a>
$a = '<a class="***" href="http://affiliateprogram.affiliates.com/?random=query_string&page=destination" attr="***">';

$b = preg_replace("/<a([^>]*)href=\"http:\/\/affiliateprogram\.affiliates\.com\/[^\"]*\"([^>]*)>/", "<a\\1href=\"http://www.mylink.com/\"\\2>", $a);

var_dump($b); // <a class="***" href="http://www.mylink.com/" attr="***">

这很简单,因为查询字符串只需要一个占位符。 .*? 通常会这样做,但是您可以通过匹配任何不是双引号的内容来使其更加具体:

$html =
preg_replace('~ href="http://affiliateprogram\.affiliates\.com/[^"]*"~i',
              ' href="http://www.mylink.com"', $html);

人们可能会四处走动,并建议使用方法,但这对于这样的任务可能是过大了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM