简体   繁体   English

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

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

I have tried searching through related answers but can't quite find something that is suitable for my specific needs. 我尝试搜索相关答案,但找不到适合我特定需求的东西。 I have quite a few affiliate links within 1,000s of articles on one of my wordpress sites - which all start with the same url format and sub-domain structure: 我的一个wordpress网站上有成千上万的文章有很多会员链接-所有链接都以相同的url格式和子域结构开头:

http://affiliateprogram.affiliates.com/

However, after the initial url format, the query string appended changes for each individual url in order to send visitors to specific pages on the destination site. 但是,在初始URL格式之后,查询字符串将针对每个单独的URL附加更改,以便将访问者发送到目标网站上的特定页面。

I am looking for something that will scan a string of html code (the article body) for all href links that include the specific domain above and then replace THE WHOLE LINK (whatever the query string appended) with another standard link of my choice. 我正在寻找一种可以扫描包括上面特定域的所有href链接的html代码字符串(文章正文),然后用我选择的另一个标准链接替换“全链接”(无论附加查询字符串如何)。

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

gets replaced with 被替换为

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

I would ideally like to do this via php as I have a basic grasp, but if you have any other suggestions I would appreciate all input. 理想情况下,我希望通过php进行此操作,因为我有基本的了解,但是如果您有任何其他建议,我将不胜感激。

Thanks in advance. 提前致谢。

<?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 http://ideone.com/qaEEM

Use a regular expression such as: 使用正则表达式,例如:

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
  )
);

output 输出

<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="***">

That's quite simple, as you only need a single placeholder for the querystring. 这很简单,因为查询字符串只需要一个占位符。 .*? would normally do, but you can make it more specific by matching anything that's not a double quote: 通常会这样做,但是您可以通过匹配任何不是双引号的内容来使其更加具体:

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

People will probably come around and recomend a longwinded approach, but that's likely overkill for such a task. 人们可能会四处走动,并建议使用方法,但这对于这样的任务可能是过大了。

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

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