0

I want to transform the following:

1. From:
<img class="lazyjs bbcodeImage" src="//google.com/blank.gif" data-original="http://google.com/poster.jpg" alt="image" />

1. To:
<img src="http://domain.com/poster.jpg" />

2. From:
<a rel="nofollow" href="/confirm/url/aHR0cDovL2dvb2dsZS5jb20%3D/" class="ajaxLink">

2. To:
<a href="http://google.com">

Basically, I want to use the data-original for <img src. The <a href is first encoded with base64_encode, then with urlencode.

Here's what I've done at until now:

<?php
// 1
$string = '<img class="lazyjs bbcodeImage" src="//google.com/blank.gif" data-original="http://google.com/poster.jpg" alt="image" />';

echo preg_replace('/<img class="lazyjs bbcodeImage" src="\/\/google.com\/blank.gif" data-original="(.*?)" alt="image" \/>/', '<img src="$1" />', $string);

// 2
$string = '<a rel="nofollow" href="/confirm/url/aHR0cDovL2dvb2dsZS5jb20%3D/" class="ajaxLink">';

echo preg_replace('/<a rel="nofollow" href="\/confirm\/url\/(.*?)\/" class="ajaxLink">/', '<a href="$1">', $string);
?>

The problem is that on 2 I don't know how to decode the $1.

Turbo Host
  • 91
  • 1
  • 1
  • 6

2 Answers2

0

There will probably be people suggesting regex, but according to RegEx match open tags except XHTML self-contained tags that is not a proper solution. Thank god somebody made PHPquery. This way you can use selectors like you are used to in jQuery to select these attributes.

Community
  • 1
  • 1
online Thomas
  • 8,864
  • 6
  • 44
  • 85
  • If you're talking about the JavaScript's jQuery library - this is a PHP Non-HTML application, so I have to stick with the PHP only. – Turbo Host Jan 04 '16 at 11:02
  • No it's a port from the javascript version to PHP, that's why it's call PHPquery.... – online Thomas Jan 04 '16 at 11:02
  • Is it fast? I'm talking about 10,000,000 queries per day. I really don't want to include any class, but if this is the only way... – Turbo Host Jan 04 '16 at 11:07
  • @TurboHost 10 milion isn't a lot for computers nowadays. But you can still use regex if you want, I guess it is acceptable in this case. – online Thomas Jan 04 '16 at 15:30
0

Nevermind, I think I did it:

<?php
// 1
$string = '<img class="lazyjs bbcodeImage" src="//google.com/blank.gif" data-original="http://google.com/poster.jpg" alt="image" />';

echo preg_replace('/<img class="lazyjs bbcodeImage" src="\/\/google.com\/blank.gif" data-original="(.*?)" alt="image" \/>/', '<img src="$1" />', $string);

// 2
$string = '<a rel="nofollow" href="/confirm/url/aHR0cDovL2dvb2dsZS5jb20%3D/" class="ajaxLink">';

echo preg_replace_callback('/<a rel="nofollow" href="\/confirm\/url\/(.*?)\/" class="ajaxLink">/', function ($match) { return '<a href="' . base64_decode(urldecode($match[1])) . '">'; }, $string);
?>
Turbo Host
  • 91
  • 1
  • 1
  • 6