how to make RegEx to match CSS non greedy for a part of the match

Tags: php,css,regex

Problem :

I'm working on this RegEx to match and replace CSS with other CSS values. So far I have this:

$css_new = preg_replace('/('.$definition.'.*?\s'.$property.':)[^;]*([^}]*)/is','$1 '.$value.'$2',$css_temp);

The issue I have is that if I am trying to replace something in the definition ".menu" values also get replaced in the ".menu li" definition.

I was thinking of adding a "{" somewhere but I'm not having luck with it. I tried this:

$css_new = preg_replace('/('.$definition.'{.*?\s'.$property.':)[^;]*([^}]*)/is','$1 '.$value.'$2',$css_temp);

Thanks for your help.

Solution :

Just wanted to report how I solved this problem. I was able to come up with this line and have tested it extensively.

$css_new = preg_replace('/(.*^'.$definition.'\s{.*?\s'.$property.':)[^;]*([^}]*)/ism','$1 '.$value.'$2',$css_temp);

If you see something wrong with this line please let me know but I believe this solves the issue.

    CSS Howto..

    how do you center and left align columns, at the same time, in bootstrap or regular html5?

    how to set transform for div without effect on background image?

    How to create a completely flexible piano keyboard with HTML and CSS

    How to using CSS to set content of column to top not center in JSF, primefaces?

    How to use center-block and vertical align together using Bootstrap?

    How to make a text with skewed line but not letters?

    How to create a vertical clickable divider|line in bootstrap?

    How to show “…” when there's not enough space to view all the text

    CSS: How to decrease the margin of my paragraphs inside a div?

    CSS3 Challenge - How to get this gradient background pattern without using all the divs?

    How to get user submission to post to page

    How to merge .CSS files with Sass (or other tool)? [duplicate]

    How to change button colors upon click (Javascript)?

    How can I count HTML elements on a printed page using CSS?

    How to add css class?

    How to add dynamic CSS source urls in Squishit?

    Site Network Bar - Show Current Site Stats, CSS?

    How to make page layout responsive for every successive little change in window size?

    how to align divs with different width within another parent div?

    how to prevent child element from widening parent element's width in css

    How can I align centered inline-block elements correctly?

    How to remove hover effect from a column of a table

    How to move object into the screen from outside via jQuery transition

    How to use :before for the third
  • element?
  • How to add CSS to page to the bottom of the custom chrome extension

    How to use toggle css classes on an element onclick?

    How to overflow in CSS/HTML? [closed]

    How to divide a border into 4 equal parts?

    How to create spaces between elements in HTML/CSS

    How to fade a box shadow in CSS?