Jump to content

Regex problem


gijew

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!
Link to comment
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]
Link to comment
Share on other sites

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.