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? Link to comment https://forums.phpfreaks.com/topic/45732-solved-ereg/ 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]+$ Link to comment https://forums.phpfreaks.com/topic/45732-solved-ereg/#findComment-222163 Share on other sites More sharing options...
taith Posted April 5, 2007 Author Share Posted April 5, 2007 perfection! thanks :-D Link to comment https://forums.phpfreaks.com/topic/45732-solved-ereg/#findComment-222164 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. Link to comment https://forums.phpfreaks.com/topic/45732-solved-ereg/#findComment-222173 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.