Preg_match with regex expression for phone number

Please house,
i need to allow a set of number to start with + and end with digits.
eg. +38976768777
This is what i have but is not working, throwing errors too


please help out, thanks.

Your pattern is invalid.

* and $ are not modifiers.

Your pattern starts and stops at the /'s.

Also, if you do move your trailing / to the correct location, your pattern is still incorrect - it currently would allow +++++++++ as a valid phone number, also 123879872398712987391827398127398712983719827391827398127397293918837198721.


thanks @m_hutley

I already know am not getting it right, regex confuses me a whole lot, but i must say is a very powerful tool to use.
don’t just show me the correct method also throw light in how to do it.

especially /^ means start but i don’t know of ending delimeters. and i know [0-9] to be ranges of any digits but i don’t know how to put them together as a valid code

Delimiter: Things that go around the pattern to tell the regex engine where the pattern starts and stops.
More specifically, the first character SETS the delimiter - so the first character “/” says to the regex engine “Keep going until you see another /. That’s my pattern.” (Consequently, the Delimiter character cannot appear IN the pattern, which is why the first character sets the character - if you wanted to include / in the pattern, you would chose another delimiter character, like #.)

^ at the beginning of a pattern means “This must be the start of the string.”

$ at the end of a pattern means “This must be the end of the string.”

Consequently, having both ^ and $ means “The entire string must match this pattern.”

For example:
“abc123” will match the pattern “bc123”, and “bc123$” but not “^bc123” or “^bc123$” because the start of the string did not begin with a b.


wow obviously thanks for this explanation. @m_hutley

I got the gist now.

preg_match ('/^[0-9]*$/');

this code means it should start and end with digits anything from 0-9
Am i right?

what is the function of * or is it just another keyword just like b8cj?

or just a simple code will be

preg_match ('/[0-9]/');

meaning any number range is accepted without adding ^ and $

this can match “gf65483” because it must end with 3?

while “^bc123” matches “bhgdd7865” because it started with b

while “^bc123$” matches “bghfghv7773” because it began with b and ends with 3

the middle characters are irrelevant because we are not using range like [0-9] [a-z] [A-Z]

This cheat sheet should explain that and some other questions.

1 Like

No. Without a character class, every character is explicitly relevant, and must be present in the match.

“bc123$” reads as:
“The letter b. Followed by the letter c. Followed by the number 1. Followed by the number 2. Followed by the number 3, at the end of the string” (or line, but… topic beyond scope.)

“bc123$” would find a match in:

If I wanted to find ‘any string that contains one of: b,c,1,2,3 anywhere in the string’, I would use the pattern "[bc123]" as my test.

Consider the following:
“I want to replace all instances of the words ‘hiss’ and ‘kiss’ with the word ‘blank’”. My regex for this match may be "[hk]iss" (actually it would probably be something like "(^|\s)[hk]iss($|\s)", but… walk before you run.)

So, what does preg_match('/[0-9]/'); tell your script to look for?

1 Like

It told it to look for 0,1,2,3,4,5,6,7,8,9 anywhere they are found in the string

It told it to find exactly 1 of those somewhere in the string. Correct.

Go back to your original statement:

Knowing what you know now, and using the cheatsheet Gandalf provided, give me the answer to your question.

am yet to read @Gandalf sheet, was still studying yours, but give me few minutes


I think the above code will do the magic :smile:

thanks @Gandalf

1 Like

Close. You tried to run a little bit before you walked there. :wink: You copied my pattern without understanding it.

Put this sentence into pattern form:
“From the start of the string, I want a plus sign. Followed by digits 0-9, repeated one-or-more times, Followed by the end of the string.”
(Hint: you’ll need the Quantifiers section of the cheat sheet.)

you are very correct i knew i got the answer but don’t know what (^|\s) actually means :smile:

what will be at the end of the string? which word or letters?

the quantifiers said + is a quantifier meaning if i use it like that am telling the script to add instances, which will result to Z+ meaning ZZ

so for me to make it work i should cast + into a string so the code wont see it as a quantifer but as a letter or string


What i did:
I started the pattern with ^
Then i escaped plus to act like string instead of a quantifier

then i added range or 0-9

then i added the quantifer plus which means repeat one or more times

then i ensure it ended as a digit which is what /d does

but /s ends it as string

okay, we’re getting closer.
#1: escaping is done with a \, not a /.
#2: The full expression of what you have written there, if you switched all the /'s to \'s, is as follows:
“From the beginning of the string, start a subpattern. Literal +, literal ), 1 or more 0-9, followed by a digit.”

Which is close, but:

  • We don’t want a subpattern for the +. (and besides, because you escaped the ), the subpattern never closes!)

  • We want to make sure that this pattern is the whole string. (Otherwise, we could say “+12asdbasdas” is a valid phone number.) (Hint: See post #4.)

1 Like