简体   繁体   中英

String replace with regex in PHP

I want to modify the contents of an html file with php. 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.

$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. 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.

Example using 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:

<?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

Search for:

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

and replace with:

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

http://regex101.com/r/zP2tV9

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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