Perad Posted November 2, 2010 Share Posted November 2, 2010 Hi, Honestly, I am trying to pick up regex using nettuts. (http://net.tutsplus.com/tutorials/php/regular-expressions-for-dummies-screencast-series/) However I still haven't got very far and something need's fixing today. I need a regex that does the following. $html = '...'; // Lots of HTML $regex = '{absolutely anything}color: #{6 digits - [0-9][a-f][A-F]};{absolutely anything}'; I will then use this to force users to have a certain color on their HTML elements. Quote Link to comment Share on other sites More sharing options...
JAY6390 Posted November 2, 2010 Share Posted November 2, 2010 ~\bcolor:\s+?#[0-9a-f]{3,6};~ Quote Link to comment Share on other sites More sharing options...
.josh Posted November 4, 2010 Share Posted November 4, 2010 @JAY6390: I'd probably use \s* instead of \s+? because you don't technically need a space. But even if at least one space was certain, you don't actually need that ? as all it does is make it lazy, not optional, and it's matching something specific, not a wide range (like a dot). Also, your char class only has lowercase a-f. Since capitals are allowed, you either need to throw A-F in there or use i modifier. Also, {3,6} probably won't work either...that would allow for instance 1234 which is not valid...would probably need to instead use alternation like ~\bcolor:\s*#([0-9a-f]{3}|[0-9a-f]{6});~i and one last thing, i'd consider wrapping that whole thing in matching for <...> or style="..." to more specifically put it onto a context where you'd find it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.