简体   繁体   中英

PHP Using Regular Expressions to change css propertyin html string

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="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*([;"'])

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