如何在PHP中的img标签上使用preg_replace类和src?

你有Truong IT

我有一个例子:

<a href="http://test.html" class="watermark" target="_blank">
   <img width="399" height="4652" src="http://test.html/uploads/2013/10/10.jpg" class="aligncenter size-full wp-image-78360">
</a>

我使用preg_replace更改标签的值类和img标签的src

$content = preg_replace('#<a(.*?)href="([^"]*/)?(([^"/]*)\.[^"]*)"([^>]*?)><img(.*?)src="([^"]*/)?(([^"/]*)\.[^"]*)"([^>]*?)></a>#', '<a href=$2$3 class="fancybox"><img$1src="http://test.html/uploads/2013/10/10_new.jpg"></a>', $content); 

结果如何呢?

<a href="http://test.html" class="fancybox" target="_blank">
    <img width="399" height="4652" src="http://test.html/uploads/2013/10/10_new.jpg" class="aligncenter size-full wp-image-78360">
</a>
SmokeyPHP

正则表达式,正如每天在SO上多次提到的那样,并不是用于HTML操作的最佳工具-幸运的是我们有DOMDocument对象!

如果仅提供该字符串,则可以进行如下更改:

$orig = '   <a href="http://test.html" class="watermark" target="_blank">
                <img width="399" height="4652" src="http://test.html/uploads/2013/10/10.jpg" class="aligncenter size-full wp-image-78360">
        </a>';
$doc = new DOMDocument();
$doc->loadHTML($orig);
$anchor = $doc->getElementsByTagName('a')->item(0);
if($anchor->getAttribute('class') == 'watermark')
{
    $anchor->setAttribute('class','fancybox');
    $img = $anchor->getElementsByTagName('img')->item(0);
    $currSrc = $img->getAttribute('src');
    $img->setAttribute('src',preg_replace('/(\.[^\.]+)$/','_new$1',$currSrc));
}
$newStr = $doc->saveHTML($anchor);

否则,如果您使用的是完整的文档HTML源代码,请执行以下操作:

$orig = '<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title></title>
</head>
<body>
    <a href="http://test.html" class="watermark" target="_blank">
        <img width="399" height="4652" src="http://test.html/uploads/2013/10/10.jpg" class="aligncenter size-full wp-image-78360">
    </a>
    <span>random</span>
    <a href="http://test.html" class="watermark" target="_blank">
        <img width="399" height="4652" src="http://test.html/uploads/2013/10/10.jpg" class="aligncenter size-full wp-image-78360">
    </a>
    <a href="#foobar" class="gary">
        <img src="/imgs/yay.png" />
    </a>
</body>
</html>';
$doc = new DOMDocument();
$doc->loadHTML($orig);
$anchors = $doc->getElementsByTagName('a');
foreach($anchors as $anchor)
{
    if($anchor->getAttribute('class') == 'watermark')
    {
        $anchor->setAttribute('class','fancybox');
        $img = $anchor->getElementsByTagName('img')->item(0);
        $currSrc = $img->getAttribute('src');
        $img->setAttribute('src',preg_replace('/(\.[^\.]+)$/','_new$1',$currSrc));
    }
}
$newStr = $doc->saveHTML();

尽管对于大脑锻炼,我已经提供了一个正则表达式解决方案,因为这是原始问题,有时DOM文档可能会使代码过多(尽管仍然更可取)

$newStr = preg_replace('#<a(.+?)class="watermark"(.+?)<img(.+?)src="(.+?)(\.[^.]+?)"(.*?>.*?</a>)#s','<a$1class="fancybox"$2<img$3src="$4_new$5"$6',$orig);

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在php preg_replace中剥离$ {1}?

如何在将HTML标签保持在PHP中的同时替换HTML标签内部的空格(preg_replace)?

如何在PHP中使用preg_replace()?

使用 preg_replace [PHP] 获取 img 标签详细信息

如何使用preg_replace和regex表达式替换php文件中的代码块?

如何使用preg_replace PHP从文件中删除文本

PHP-如何使用preg_match_all获取具有特定类名称的img标签的src?

如何在 <img src=""> 标签中插入 <?php echo $image;?>

preg_replace img src到data-src堆栈溢出(PHP)

如何在PHP中使用preg_replace或preg_match移动子字符串?

如何使用preg_replace更改另一个php / txt文件中的变量名称和值

Instagram主题标签和preg_replace

如果preg_replace <img src =“”>超过5次,则使用其id =“”

如何在PHP中将\ n \ r与preg_replace匹配

如何在文本中preg_replace <br />和> <h2>&nbsp; </ h2>?

标签preg_replace

如何在preg_replace中使用数组和正则表达式作为模式?

如何用preg_replace和PHP替换特殊字符?

span标签中的preg_replace字符

在PHP和regex中同时执行Preg_replace操作

使用preg_replace删除<script>标签

数组:使用 preg_replace 和排序过滤 [PHP - WordPress]

如何在php中使用preg_replace用每个唯一值替换多次出现

我如何在站点URL后使用PHP函数preg_replace()命名.txt文件

如何在带有{{mustache}}的PHP中使用preg_replace

如何在 PHP 中使用 preg_replace 用元字符替换表达式?

如何在尺寸表中使用preg_replace并在PHP中进行替换?

如何在 PHP 中使用 preg_replace 来做这个例子?

如何在php中使用preg_replace替換url http:/\/\example.com/\/\home