Link to home
Start Free TrialLog in
Avatar of lfirth1959
lfirth1959Flag for Australia

asked on

RegEx ASP to PHP Conversion

I want to convert the following function I use in asp/vbscript to PHP. All it does is take a 10 digit phone number like 0312341234 and adds spaces to it so that it's more readable on the screen like 03 1234 1234.
 
Function PhoneNum(intValue)

      Dim strRetValue, RegEx
      Set RegEx = New RegExp
      RegEx.pattern = "(\d{2})(\d{4})(\d{4})"
      RegEx.Global = True
      
      strRetValue = RegEx.Replace(intValue, "$1" & " " & "$2" & " " & "$3")
      
      PhoneNum = strRetValue
      
End Function
ASKER CERTIFIED SOLUTION
Avatar of ddrudik
ddrudik
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lfirth1959

ASKER

<?php
function phoneNum($string) {
      $pattern = '/(\d{2})(\d{4})(\d{4})/';
      $repl = '$1 $2 $3';
      echo preg_replace($pattern, $repl, $string );
}
?>
<?php
$phoneNum = "0312341234";
phoneNum($phoneNum);
?>