AFT
AFT

Reputation: 115

Converting a Javascript regular expression to preg_match() compatible

I have this code from a javascript

/+\uFF0B0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9u/

after some read about php & \u support I convert it to \x

/\+\x{FF0B}0-9\x{FF10}-\x{FF19}\x{0660}-\x{0669}\x{06F0}-\x{06F9}/u

but still I'm not able to use it in php

$phoneNumber = '+911561110304';
$start = preg_match('/\+\x{FF0B}0-9\x{FF10}-\x{FF19}\x{0660}-\x{0669}\x{06F0}-\x{06F9}/u', $phoneNumber,$matches);

the matches will be null!

how to fix this?

Upvotes: 0

Views: 434

Answers (1)

Alan Moore
Alan Moore

Reputation: 75232

It looks like you want to match an ASCII plus sign or its Japanese Halfwidth equivalent, followed by one or more digits from a few different writing systems. But, as @mario observed, you seem to be missing some square brackets. The JavaScript version probably should be:

/[+\uFF0B][0-9\uFF10-\uFF19\u0660-\u0669\u06F0-\u06F9]+/

(I couldn't see any reason for the u at the end, so I dropped it.) The PHP version would be:

'/[+\x{FF0B}][0-9\x{FF10}-\x{FF19}\x{0660}-\x{0669}\x{06F0}-\x{06F9}]+/u'

Of course, this will allow a mix of ASCII, Arabic and Halfwidth characters in the same number. If that's a problem, you'll need to break it up a bit. For example:

'/\+(?:[0-9]+|[\x{0660}-\x{0669}]+|[\x{06F0}-\x{06F9}]+)|\x{FF0B}[\x{FF10}-\x{FF19}]+/u'

Upvotes: 1

Related Questions