M.Awais
M.Awais

Reputation: 31

How can i skip 0041 and 0 before storing phone number in controller variable using laravel 5.4

I need to remove 0041 and 0 before Number because some users entered 0041 or 0 before 12345678. So, i just add 0041 static

$order->phone       =        "0041" . $request->phone;

So, if user entered 0041 or 0 before number it should be skipped and number will stored 004112345678.

Please give me any idea for it. Thanks

Upvotes: 1

Views: 162

Answers (3)

problem
problem

Reputation: 34

if(preg_match("/^(0041|0)(?P<test>\d+)/", $number, $matches)){
  $phone = "0041" . $matches['test'];
}

Upvotes: 1

Niklesh Raut
Niklesh Raut

Reputation: 34914

You can use str_pad with fixed length 12

<?php
//$month = "004112345678";
$month = "012345678";
echo str_pad($month, 12, "0041xxxxxxxx", STR_PAD_LEFT);
?>

Live Demo

Upvotes: 1

CodeGodie
CodeGodie

Reputation: 12132

Use substr to check for the first digits. Use it in a conditional as such:

if(substr($phone, 0, 4) !==  "0041"){
    $phone = "0041" . $phone;
}

Upvotes: 1

Related Questions