taith Posted April 5, 2007 Share Posted April 5, 2007 sorry, i know very little of ereg... but i'm trying to make a function to determine if a string is a hex or not... heres what i got sofar... function is_hex($string){ if($string{0}=="#") $string=substr($string,1); if(strlen($string)!='6') return false; if(!ereg("[a-fA-F0-9]", $string)) return false; return true; } if(is_hex("#asdf89")) echo 'true'; else echo 'false'; any idea why it keeps returning true? Quote Link to comment Share on other sites More sharing options...
effigy Posted April 5, 2007 Share Posted April 5, 2007 You're not using anchors or quantifiers; the expression is looking for one a-fA-F0-9 anywhere inside the string. ^[a-fA-F0-9]+$ Quote Link to comment Share on other sites More sharing options...
taith Posted April 5, 2007 Author Share Posted April 5, 2007 perfection! thanks :-D Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted April 5, 2007 Share Posted April 5, 2007 if(!ereg("[a-fA-F0-9]{6}", $string)) Using that you could also eliminate the check for string length above the ereg call. 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.