Somk
Somk

Reputation: 12047

Validate that a string starts with specified numbers then a certain number of digits

$output = preg_replace( '/[^0-9]/', '', $string );

How can I change the above line to only accept 11 digit long numbers that must start with either 01, 02, 03, 05, 07 or 08, but NOT 04, 06, 09?

Upvotes: 0

Views: 506

Answers (4)

Vinod
Vinod

Reputation: 4872

Try this:

^0[1-3578]\d{9}$

Regular expression visualization

0-Zero [^469]-Number except 469 \d-Any Number

Upvotes: 0

Philip G
Philip G

Reputation: 4104

Use following pattern:

 /^0[123578]{1}[0-9]{9}$/

Upvotes: 3

Rhys
Rhys

Reputation: 1491

Try this:

/^0[123578][0-9]{9}$/

Upvotes: 0

Sabuj Hassan
Sabuj Hassan

Reputation: 39355

Try this one:

$output = preg_replace( '/^0[1-3578]\d{9}/', '', $string );

\d{9} means 9 digits as you have already 2 digits back.

Upvotes: 2

Related Questions