简体   繁体   English

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

[英]String replace with regex in PHP

I want to modify the contents of an html file with php. 我想用php修改html文件的内容。 I am applying style to img tags, and I need to check if the tag already has a style attribute, if it has, I want to replace it with my own. 我正在将样式应用于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 is the html source code I receive. $ theData是我收到的html源代码。 If a style attribute has not been found, I successfully insert my own style, but I think the problem comes when there is already a style attribute defined so my regex is not working. 如果未找到样式属性,则可以成功插入自己的样式,但是当已经定义了样式属性,因此我的正则表达式无法正常工作时,我认为问题就来了。

I want to replace the style attribute with everything inside it, with my new style attribute. 我想用我的新样式属性将样式属性替换为其中的所有内容。 How should my regex look? 我的正则表达式看起来如何?

Instead of using regex for this, you should use a DOM parser. 与其使用正则表达式,不如使用DOM解析器。

Example using DOMDocument : 使用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;">

?>

Here is the regexp variant of solving this problem: 这是解决此问题的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;

prints: 印刷品:

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

Regex expression: 正则表达式:

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

Usage: 用法:

$1$3

Example: 例:

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

Search for: 搜索:

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

and replace with: 并替换为:

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

http://regex101.com/r/zP2tV9 http://regex101.com/r/zP2tV9

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

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