Jump to content

Begging Help - Convert this to regex? Pretty Please!


Perad

Recommended Posts

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.

@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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.