简体   繁体   中英

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:

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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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