Replacing the Value of href in PHP

Asked 2 years ago, Updated 2 years ago, 104 views

What should I do to replace all href values with $url if I have html like the following?

Please let me know if you know more.Thank you for your cooperation.

<div class="article_image"><a href="http://www.amazon.co.jp/PENTAX-%E3%83%9F%E3%83%A9%E3%83%BC%E3%83%AC%E3%82%B9%E4%B8%80%E7%9C%BC-%E3%82%BA%E3%83%BC%E3%83%A0%E3%83%AC%E3%83%B3%E3%82%BA%E3%82%AD%E3%83%83%E3%83%88-STANDARD-06239/dp/B00MFD07OO/ref=sr_1_1/375-7893352-7638937?ie=UTF8&qid=1457086894&sr=8-1&keywords=Q-S1"><img class="article_img" src="http://ecx.images-amazon.com/images/I/41Rs3BwRg2L._AA160_.jpg" /></a></div><div class="article_image_text article_product_info"><p class="article_product_brand"><a href="http://www.amazon.co.jp/PENTAX-%E3%83%9F%E3%83%A9%E3%83%BC%E3%83%AC%E3%82%B9%E4%B8%80%E7%9C%BC-%E3%82%BA%E3%83%BC%E3%83%A0%E3%83%AC%E3%83%B3%E3%82%BA%E3%82%AD%E3%83%83%E3%83%88-STANDARD-06239/dp/B00MFD07OO/ref=sr_1_1/375-7893352-7638937?ie=UTF8&qid=1457086894&sr=8-1&keywords=Q-S1">PENTAX ミラーレス一眼 Q-S1 ズームレンズキット [標準ズーム 02 STANDARD ZOOM] ゴールド 06239</a></p><p class="article_product_price">¥ 28,205 </p><div class="btn_item_area clearfix"><a href="http://www.amazon.co.jp/PENTAX-%E3%83%9F%E3%83%A9% E3% 83% BC% E3% 83% AC% E3% 82% B9% E4% E8% B8% E7% 9C% BC-% E3% 83% BA% E3% 83% A0% E3% 83% AC% E3% 82% BA% E3% 83% 83% AD% E3% 83% 83% 83% E3% 83% 88-STANDARD-06239/dp/B0038931_MF_ref=37535

php html regular-expression

2022-09-30 20:12

2 Answers

Does it mean that the HTML tag is a string?
If the tag is stored in $html_code, you can replace it as follows.

$url='String you want to replace';
$html_code='<div class="article_image">a href="http://www.amazon.co.jp/~omitted~&keywords=Q-S1"target="_blank"class="btn_item btn_shop">Buy from the site</a>>div;
$output=preg_replace("/(<a href=\").*?(\".*?>)/", "$1$url$2", $html_code);
echo$output;


2022-09-30 20:12

Should I replace the href with the variable $url?

<?php

// Your code goes here

$html = '<div class="article_image"><a title="test" href=\'http://www.amazon.co.jp/PENTAX-%E3%83%9F%E3%83%A9%E3%83%BC%E3%83%AC%E3%82%B9%E4%B8%80%E7%9C%BC-%E3%82%BA%E3%83%BC%E3%83%A0%E3%83%AC%E3%83%B3%E3%82%BA%E3%82%AD%E3%83%83%E3%83%88-STANDARD-06239/dp/B00MFD07OO/ref=sr_1_1/375-7893352-7638937?ie=UTF8&qid=1457086894&sr=8-1&keywords=Q-S1\'><img class="article_img" src="http://ecx.images-amazon.com/images/I/41Rs3BwRg2L._AA160_.jpg" /></a></div><div class="article_image_text article_product_info"><p class="article_product_brand"><a href="http://www.amazon.co.jp/PENTAX-%E3%83%9F%E3%83%A9%E3%83%BC%E3%83%AC%E3%82%B9%E4%B8%80%E7%9C%BC-%E3%82%BA%E3%83%BC%E3%83%A0%E3%83%AC%E3%83%B3%E3%82%BA%E3%82%AD%E3%83%83%E3%83%88-STANDARD-06239/dp/B00MFD07OO/ref=sr_1_1/375-7893352-7638937?ie=UTF8&qid=1457086894&sr=8-1&keywords=Q-S1">PENTAX ミラーレス一眼 Q-S1 ズームレンズキット [標準ズーム 02 STANDARD ZOOM] ゴールド 06239</a></p><p class="article_product_price">¥ 28,205 </p><div class="btn_item_area clearfix"><a href="http://www.amazon.co.jp/PENTAX-%E3%83%9F%E3%83%A9%E3%83%BC%E3%83%AC%E3%82%B9%E4%B8%80%E7%9C%BC-%E3%82%BA%E3%83%BC%E3%83%A0%E3%83%AC%E3%83%B3%E3%82%BA%E3%82%AD%E3%83%83%E3%83%88-STANDARD-06239/dp/B00MFD07OO/ref=sr_1_1/375-7893352-7638937?ie=UTF8&qid=1457086894&sr=8-1&keywords=Q-S1" target="_blank" class="btn_item btn_shop"> Buy from site </a></div>';
$url='http://example.com/';

$result=preg_replace(
    // regexp
    '/(<a.*?href=(?\'|")([^\']+?)(?\'|").*?>/',
    // replacement
    '$1'.$url.'$3',
    // input
    $html
);

echo$result;

http://sandbox.onlinephpfunctions.com/code/6ad18c9538f82e45fda5c5627ece4df188edb33a

Consider preg_replace_callback to customize the replacement process for each href content.
For $1, see the php documentation or https://stackoverflow.com/questions/2888417/.


2022-09-30 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.