Home > Mobile >  Get 06 number from string how to do
Get 06 number from string how to do

Time:11-25

I've a question. I've a string that can change everytime.

  $string = "This is Henk 0612345678";

This is a example. I want to get the 06 number from the string.

So i can use to make a whatsapp api link to whatsapp directly.

Can i do this with preg_replace or is there a easier way?

This is an example i want.

 $string = "This is Harry 0645668901";
 $number = "0645668901";

If i get the number from the string it has to be converted to: 31645668901 (the zero has to be removed ant 31 has to be added first in the string)

 <a href="https://api.whatsapp.com/send?phone=31645668901">Click here</a>

If there is no 06 number in the string i dont want to display the link.

Can anyone help me and give me advice ?

Kind regards, Herman

CodePudding user response:

First you need to remove the words from the string and keep the numbers only.

You can do this.

$string = "This is Henk 0612345678";

$number = preg_replace('/[^0-9.] /', '', $string);

echo $number;

Output: 0612345678

Then you need to remove the first character which is 0 and add 31 to the beginning of the number so we do this.

$number = substr($number, 1) ;
$number = 31 . $number;

Output: 31612345678

And finaly.

 <a href="https://api.whatsapp.com/send?phone=<?php echo $number; ?>">Click here</a>

  • Related