Jump to content

Begging Help - Convert this to regex? Pretty Please!


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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.