Jump to content

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!

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.