Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6686879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:13:23+00:00 2026-05-26T05:13:23+00:00

I have html eg below and would like to use regular expressions to change

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T05:13:23+00:00Added an answer on May 26, 2026 at 5:13 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a HTML document with the below setup: <div class=main-div style=padding: 5px; border:
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
BACKGROUND: I would like to have small labels in columns of a table. I'm
I have html code <td id=MyNode>TextNode</td> and would like to make it as <td
I'm working on a forum. I would like to simple have php or html
http://www.grails.org/doc/latest/ref/Constraints/validator.html I have a project where I would like users to log in with
I have some HTML that looks like the following: http://jsfiddle.net/KbqHa/ I would like this
I have an idea for a layout I would like to use, but I
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have the HTML given below: <ul id=thumbsPhotos> <li src=/images/1alvaston-hall-relaxing-lg.jpg onclick=updatePhoto (this.title)><img src=/images/1alvaston-hall-relaxing-sl.jpg width=56

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.