Jump to content

Archived

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

gijew

Regex problem

Recommended Posts

I'm trying to validate as much as I can html color codes that come through. 

What I'm trying to achieve is...
1.  Validate if 6 alpha numeric characters are being sent without the # sign.
2.  If more or less than 6 alpha numeric characters are being sent, default to xxx.
3.  If any other character is being sent other than 6 alpha numeric characters, default to xxx.

End result, no error messages, just defaults unless a valid 6 character alpha numeric string is found.

I know that I'm going about this wrong so I started using some regex again which I suck at.  If you require, I can show what I have.  It's not right, it's not good and it's not clean but it's what I know. 

Can someone help me out with a good search and replace regex here?  Thanks!

Share this post


Link to post
Share on other sites
[code]
<pre>
<?php

$tests = array(
'FFFFFF',
'#FFFFFF',
'#ff',
'fff',
'asdkhyuy',
'#artyui',
'1234567',
'',
'000000'
);

foreach ($tests as &$test) {
$test =
preg_match('/^[a-f\d]{6}$/i', $test) ?
$test :
'xxx' ;
}

print_r($tests);

?>
</pre>
[/code]

Share this post


Link to post
Share on other sites

×

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.