使用下拉列表格式化电话号码

I'm new to php and i wanna format mobile phone numbers... thing is i have a dropdown box that contains dial code and I have another box that a user would input his/her mobile number. I wanna do the following...

  1. Check for and remove the leading '+'
  2. Check for and remove the leading '0'
  3. Check for and remove the dialing code if it is the same with dial code in dropdown.
  4. Append the selected dial code at the beginning of the string...

Here is my code... Seems to work fine but somehow it removes the last digit if user enters a number starting with '0'

$phNo = $_POST['RegPhone'];

if (substr($phNo, 0, 1) == "+") {
    $phNo = ltrim($phNo, '+');
}

if (substr($phNo, 0, 1) == "0")  {
    $phNo = ltrim($phNo, '0');
}

if (substr($phNo, 0, strlen($_POST['RegDialCode'])) == $_POST['RegDialCode'])  {
    $phNo = ltrim($phNo, $_POST['RegDialCode']);
}

$phNo = $_POST['RegDialCode']. $phNo;
$phNo = $_POST['RegPhone'];

$phNo = str_replace("+", "", $phNo);  // Find + and replace if present

if (strpos($ph, '0') == 0)  {   // check if 0 found and it at first postition
    $phNo = ltrim($phNo, '0');
}

if (substr($phNo, 0, strlen($_POST['RegDialCode'])) == $_POST['RegDialCode'])  {
    $phNo = ltrim($phNo, $_POST['RegDialCode']);
}

$phNo = $_POST['RegDialCode']. $phNo;