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
.
答案 0 :(得分:0)
可能会有人建议regex
,但根据RegEx match open tags except XHTML self-contained tags,这不是一个正确的解决方案。感谢上帝有人PHPquery。通过这种方式,您可以像在jQuery中一样使用选择器来选择这些属性。
答案 1 :(得分:0)
没关系,我想我做到了:
<?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);
?>