开发者

PHP Using Regular Expressions to change css propertyin html string

开发者 https://www.devze.com 2023-04-12 17:03 出处:网络
I have html eg below and would like to use regular expressions to change the font-size on the fly. <p><span style=\"font-size: small;\"><img style=开发者_JAVA技巧\"float: left;padding:

I have html eg below and would like to use regular expressions to change the font-size on the fly.

<p><span style="font-size: small;"><img style=开发者_JAVA技巧"float: left;padding:5px" title="Ikechukwu to host Lagos Smirnoff party" src="afrostarpics/586.jpg" alt="Ikechukwu to host Lagos Smirnoff party" width="200" height="300">Smirnoff held a party like never seen before in East, North and West Africa on Friday 23rd September 2011, when they held a “Kick off” party for the Smirnoff Midnight Cruise.&nbsp; The grand party held in the populous West African nation of Nigeria was so successful that it left revellers literally begging for more.</span></p>

this is the regular expression i have

    <?php 
$bd=$nobj->body;//html string to change
$selector="font-size"; //selector to change
$property="1.2em"; //new value
 preg_replace('/('.$selector.'\s*\{[\w\s:\-;\(\)#]*)('.$property.'\s*:)([^;\}]+)(;|\})/Ui', '$1 $4', $bd); 
echo $bd;
?>

any ideas as to why this is not working


If you're set on using regex for this, you'll probably find this pattern more effective:

$bd = preg_replace('/\b'.$selector.'\s*:[^;}"\']*/i', $selector.':'.$property, $bd);

That should do what you're trying to do at least 99% of the time.

However, I want to point out that you seem to be trying to use regex to parse CSS embedded inside HTML, which is not usually something regex is good at. Even with this simple task, to solve it in a robust way you would have to distinguish between HTML attribute values and other text, which means you would have to (at least partially) parse the HTML. Doing that with regex is usually a Bad Idea, but it's incredibly simple for jQuery (client-side) or HTML Purifier (server-side).

Why is regex such a dangerous tool for this? Well, although the task itself is fairly simple, you probably don't know in advance what the plain-text part is going to say, and regex can't tell the difference between plain text and HTML. So if the phrase "font-size:" appears in your plain text (meaning the Smirnoff held a party like... part), it will probably break any sane regex. Unlikely? Yes, but it's still a potential bug.


One possible reason is that preg_replace returns the modified string, so you have to save and use the return value - something like this will work if the regex is correct:

$bd = preg_replace('/('.$selector.'\s*\{[\w\s:\-;\(\)#]*)('.$property.'\s*:)([^;\}]+)(;|\})/Ui', '$1 $4', $bd);
echo $bd;


Your regular expression looks overly complicated to me.

You could always parse the results as XML, and then modify the attributes, but assuming you still want REGEX, I think something like this should suffice:

$old = '<p><span style="font-weight:bold;color:blue;font-size: small;"><img style="float: left;padding:5px" title="Ikechukwu to host Lagos Smirnoff party" src="afrostarpics/586.jpg" alt="Ikechukwu to host Lagos Smirnoff party" width="200" height="300">Smirnoff held a party like never seen before in East, North and West Africa on Friday 23rd September 2011, when they held a “Kick off” party for the Smirnoff Midnight Cruise.&nbsp; The grand party held in the populous West African nation of Nigeria was so successful that it left revellers literally begging for more.</span></p>';
$selector      = 'font-size';
$desired_value = '1\.2em';
$new = preg_replace(
    '/\b(' . $selector . ')\s*:\s*(?!' . $desired_value . ').+\s*([;"\'])/Ui',
    '\1:' . stripslashes($desired_value) . '\2',
    $old
);

For reference, with the values you gave, that gives an output regex of:

\b(font-size)\s*:\s*(?!1\.2em).+?\s*([;"'])
0

精彩评论

暂无评论...
验证码 换一张
取 消