繁体   English   中英

在PHP中用正则表达式替换字符串

[英]String replace with regex in PHP

我想用php修改html文件的内容。 我正在将样式应用于img标签,并且需要检查标签是否已经具有style属性,如果有,我想用自己的标签替换它。

$pos = strpos($theData, "src=\"".$src."\" style=");
    if (!$pos){
        $theData = str_replace("src=\"".$src."\"", "src=\"".$src."\" style=\"width:".$width."px\"", $theData);
    }
    else{
        $theData = preg_replace("src=\"".$src."\" style=/\"[^\"]+\"/", "src=\"".$src."\" style=\"width: ".$width."px\"", $theData);
    }

$ theData是我收到的html源代码。 如果未找到样式属性,则可以成功插入自己的样式,但是当已经定义了样式属性,因此我的正则表达式无法正常工作时,我认为问题就来了。

我想用我的新样式属性将样式属性替换为其中的所有内容。 我的正则表达式看起来如何?

与其使用正则表达式,不如使用DOM解析器。

使用DOMDocument的示例:

<?php
$html = '<img src="http://example.com/image.jpg" width=""/><img src="http://example.com/image.jpg"/>';

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->loadHTML('<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />'.$html);
$dom->formatOutput = true;

foreach ($dom->getElementsByTagName('img') as $item)
{
    //Remove width attr if its there
    $item->removeAttribute('width');

    //Get the sytle attr if its there
    $style = $item->getAttribute('style');

    //Set style appending existing style if necessary, 123px could be your $width var
    $item->setAttribute('style','width:123px;'.$style);
}
//remove unwanted doctype ect
$ret = preg_replace('~<(?:!DOCTYPE|/?(?:html|body|head))[^>]*>\s*~i', '', $dom->saveHTML());
echo trim(str_replace('<meta http-equiv="Content-Type" content="text/html;charset=utf-8">','',$ret));

//<img src="http://example.com/image.jpg" style="width:123px;">
//<img src="http://example.com/image.jpg" style="width:123px;">

?>

这是解决此问题的regexp变体:

<?php
$theData = "<img src=\"/image.png\" style=\"lol\">";
$src = "/image.png";
$width = 10;

//you must escape potential special characters in $src, 
//before using it in regexp
$regexp_src = preg_quote($src, "/");

$theData = preg_replace(
    '/src="'. $regexp_src .'" style=".*?"/i',
    'src="'. $src .'" style="width: '. $width . 'px;"',
    $theData);

print $theData;

印刷品:

<img src="/image.png" style="width: 10px;">

正则表达式:

(<[^>]*)style\s*=\s*('|")[^\2]*?\2([^>]*>)

用法:

$1$3

例:

http://rubular.com/r/28tCIMHs50

搜索:

<img([^>] )style="([^"] )"

并替换为:

<img\\1style="attribute1: value1; attribute2: value2;"

http://regex101.com/r/zP2tV9

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM