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>