daveh33 Posted January 4, 2008 Share Posted January 4, 2008 I am trying to write a piece of code which will validate a variable ($value) to ensure it 12 digits, numeric only & starts 447 - I then want to define $value1 but replacing the 447 at the start with 07. I have written the below code if(ereg("^447[0-9]{9}$",$value)){ $value1 = str_replace("447", "07", $value); echo $value1; } It works ok - but it replaced ALL instances of 447 - is there a way I can code it so that it only replaces the 447 at the start of the string? Quote Link to comment https://forums.phpfreaks.com/topic/84464-solved-str_replace-ereg-validation/ Share on other sites More sharing options...
rajivgonsalves Posted January 4, 2008 Share Posted January 4, 2008 use ereg_replace function directly http://php.net/ereg_replace Quote Link to comment https://forums.phpfreaks.com/topic/84464-solved-str_replace-ereg-validation/#findComment-430337 Share on other sites More sharing options...
Daniel0 Posted January 4, 2008 Share Posted January 4, 2008 Use the PCRE functions, they're faster than ereg. Quote Link to comment https://forums.phpfreaks.com/topic/84464-solved-str_replace-ereg-validation/#findComment-430339 Share on other sites More sharing options...
pkSML Posted January 4, 2008 Share Posted January 4, 2008 if(ereg("^447[0-9]{9}$",$value)){ $value1 = substr($value, 3); $value1 = "07" . $value1; echo $value1; } This will trim off the first three characters, which are guaranteed to be 447 because of your conditional statement. May not be the most efficient way to do it, but, hey, it works! Quote Link to comment https://forums.phpfreaks.com/topic/84464-solved-str_replace-ereg-validation/#findComment-430344 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.