-1

I need to create a regex function to check phone numbers entered on a phone number field based on these conditions.

• if the number starts with either 6, 8 or 9 and another 7 digits • or if the number starts with either +656, +658 or +659 and another 7 digits

So basically its singapore phone number with +65 being the country code 6, 8 or 9 are the only starting digit of phone numbers.

I have tried code below but it wont work.

add_filter( 'gform_phone_formats', 'sg_phone_format' );
function sg_phone_format( $phone_formats ) {
    $phone_formats['sg'] = array(
        'label'       => 'Singapore',
        'mask'        => false,
        'regex'       => '/^[689]\d{7}$/D|/^(\+656)\d{7}$/D|/^(\+658)\d{7}$/D|/^(\+659)\d{7}$/D',
    );
 
    return $phone_formats;
}

Thank you!

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
Wilson C
  • 9
  • 1
  • 1
    Does this answer your question? [Singapore Mobile Number RegEx](https://stackoverflow.com/questions/42478799/singapore-mobile-number-regex) – Christian Baumann Oct 09 '20 at 08:50

2 Answers2

0

I would use this regex pattern for Singapore numbers:

^(?:\+65)?[689][0-9]{7}$

Sample script:

$number = "+6587654321";
if (preg_match("/^(?:\+65)?[689][0-9]{7}$/", $number)) {
    echo "MATCH";
}
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

So basically the set condition for a regex match can be found by alternating a grouping of sets that match the occurrences of the desired character or number, like so:

<code>
//$regex = '/\+65+[?:6?8:?:9]+[0-9]{3}+[0-9]{4}/'; //wrong 

// Edited the following lines
$regex = '/(?:\+65[6|8|9]+[\d]{7})|(?:\+65[689][\-|\s|\/][0-9]{3}+[\-|\s|\/][0-9]{4})/';
$phone = '+6561234567,+6587654321,+659-432-1567,+6594321567,+659 765 4321,+658/765/1234,+659--432--1567,+6555?:?1231234'; //test string
//Match $var against a regular expression
preg_match_all($regex, $phone, $matches, PREG_SET_ORDER, 0);


//var_dump
var_dump($matches) . " \n";

//print_r
print_r ($matches) . " \n";

//echo single value array();
echo $matches[0][0];
echo "<br />";
echo $matches[1][0];
echo "<br />";
echo $matches[2][0];
echo "<br />";
echo $matches[3][0];
echo "<br />";
echo $matches[4][0];
echo "<br />";
echo $matches[5][0];
echo "<br />";
echo $matches[6][0];
</code>

The result from the above code is as follow:

<pre>
//var_dump
array(6) { [0]=> array(1) { [0]=> string(11) "+6561234567" } [1]=> array(1) { [0]=> string(11) "+6587654321" } [2]=> array(1) { [0]=> string(13) "+659-432-1567" } [3]=> array(1) { [0]=> string(11) "+6594321567" } [4]=> array(1) { [0]=> string(13) "+659 765 4321" } [5]=> array(1) { [0]=> string(13) "+658/765/1234" } }

//print_r
Array ( [0] => Array ( [0] => +6561234567 ) [1] => Array ( [0] => +6587654321 ) [2] => Array ( [0] => +659-432-1567 ) [3] => Array ( [0] => +6594321567 ) [4] => Array ( [0] => +659 765 4321 ) [5] => Array ( [0] => +658/765/1234 ) )

//echo single value array();
+6561234567
+6587654321
+659-432-1567
+6594321567
+659 765 4321
+658/765/1234
</pre>

You can easily match more personalized regular expressions by setting specific conditions to match the string by escaping additional characters with \ or any whitespace character with \s

<code>
// add more characters to regex to match +659-432-1567
$regex = '/\+65[6|8|9]+[\-][0-9]{3}+[\-][0-9]{4}/';
</code>

The result from the modified regular expression will be

<pre>
array(1) { [0]=> array(1) { [0]=> string(13) "+659-432-1567" } }

Array ( [0] => Array ( [0] => +659-432-1567 ) )

+659-432-1567 //echo one match out of six
</pre>
webmx
  • 9
  • 2
  • I think this is not correct. Your pattern would also match `+6555?:?1231234` and would not match `91234567` Please look at [quantifiers](https://www.regular-expressions.info/refrepeat.html) and [character classes](https://www.regular-expressions.info/charclass.html) Why would you split matching 7 digits into `[0-9]{3}+[0-9]{4}` ? – The fourth bird Oct 09 '20 at 11:08
  • Thanks for pointing that wrong regex, I have edited the regex to allow more flexible matching with two sets grouping to allow include `-` or white space. Now to answer your question about splitting the matching seven digits, for some **a simple answer will suffice** for others customizing **the code** is a more of personal preference, is your choice. I invite you to test the code again. `/^(\(?[0-9]{3,3}\)?|[0-9]{3,3}[-. ]?)[ ][0-9]{3,3}[-. ]?[0-9]{4,4}$/` – webmx Oct 09 '20 at 16:41
  • The pattern from your last comment does not match, see https://regex101.com/r/jAHn9X/1 The edited pattern does not match a number without the leading `+65` see https://regex101.com/r/Nz8954/1 The pattern under `add more characters` does not match, see https://regex101.com/r/XB3a69/1 Note that there is no notion in the question to match a hyphen, space or a dot. – The fourth bird Oct 10 '20 at 09:32