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

发布于 2024-12-21 11:08:10 字数 598 浏览 0 评论 0 原文

我尝试搜索相关答案,但找不到适合我特定需求的内容。我的一个 WordPress 网站上有相当多的 1,000 篇文章内的附属链接 - 它们都以相同的 url 格式和子域结构开头:

http://affiliateprogram.affiliates.com/

但是,在初始 url 格式之后,查询字符串会为每个单独的 url 附加更改以便将访问者引导至目标网站上的特定页面。

我正在寻找能够扫描 html 代码字符串(文章正文)以查找包含上述特定域的所有 href 链接,然后用我选择的另一个标准链接替换整个链接(无论附加什么查询字符串)的东西。

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

被替换为

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

我理想地希望通过 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

黑白记忆 2024-12-28 11:08:10
<?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

<?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

青春有你 2024-12-28 11:08:10

使用正则表达式,例如:

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>

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>
妄司 2024-12-28 11:08:10
$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="***">
$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="***">
巴黎盛开的樱花 2024-12-28 11:08:10

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

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

人们可能会过来并推荐冗长的 方法,但这对于这样的任务来说可能有点过分了。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文