Jump to content

[SOLVED] str_replace & ereg validation


daveh33

Recommended Posts

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?

 

 

Link to comment
https://forums.phpfreaks.com/topic/84464-solved-str_replace-ereg-validation/
Share on other sites

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!

Archived

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

×
×
  • 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.