Check Value & Edit It

Hi Guys,

I have a form which people submit how ever i need to know how to do the following please. People enter their mobile number in the UK so it will be something like 07812345678

Now i need it so the consumer enters it in this format

+447812345678

(Country Code and then the number without the first 0)

How do i code it so when they enter their number i adds 44 at the beginning and removes the first 0 in the variable?

Any help would be good.

Thanks

You’ll have to check first to make sure that the first character of the string is a “0.” if it is then you could use str_replace(); to replace it with the +44 (or other country code)
These links may help:

strpos()
str_replace()

I need some help please, I’ve managed to code it but for some reason its not working correctly.


$mobileno=$_POST['mobileno'];
$findme = '07';
$pos = strpos($mobileno, $findme);

// Note our use of ===.  Simply == would not work as expected
// because the position of 'a' was the 0th (first) character.
if ($pos==='0') {
    echo "Its At The Beginning!";
} else {
		echo "Wrong";
}

If i enter a number that has 07 at the beginning then it still echos Wrong. What have i done wrong?

Thanks

You’re almost there!

Change your if statement to:



if ($pos == 0) {
    echo "Its At The Beginning!";
} else {
        echo "Wrong";
}

and it should work for you. We do this because the strpos() returns an integer and not a string.

hope this helps!

Thank you MainStWebGuy! It didnt work at first but i changed it to ‘===’ instead of ‘==’ and it works fine now :slight_smile:

Can you take a look at my other thread please.

awesome, and yeah i’ll take a look. What’s the link?

Thank you.