0

I searched for a lot of preg_matches but they all didn't work. I looked a lot in my code but can't find a problem there.

So I decided to make a simple preg_match to see if my code worked or if it was the preg_match that made the problem.

So if im right the code preg_match("[s-0-9+]{6,12]", $tel) should be true if tel was 0612345678 or 06-12345678 or +31 123456 right? cause that doesn't seem to work. Is it my preg_match?

If not I'll change my code and if that doesn't work I will make another question were I put my php code in.

edit got the working code with help in the comments now preg_match("/[\s0-9]{6,12}/", $tel)

Minegolfer
  • 276
  • 4
  • 18
  • 4
    PHP's `preg_match` function requires delimiters; try: `preg_match('/[s-0-9+]{6,12}/', $tel)` - though that won't match many valid phone numbers. – CD001 May 18 '17 at 14:35
  • 3
    I am no regex expert, but the fact the enclosing `{` dont not have a matching `}` shouts at ME – RiggsFolly May 18 '17 at 14:36
  • still not working, should that code work on 1 of the numbers I gave? cause it doesn't. Guess its just my code then? – Minegolfer May 18 '17 at 14:37
  • 1
    Not convinced by that `s-` in the first block either, nor the `+`... if that's meant to be whitespaces then that should probably be `[\s0-9]{6,12}` – CD001 May 18 '17 at 14:37
  • @RiggsFolly wow I didnt even see that, I actually made a stupid typo – Minegolfer May 18 '17 at 14:37
  • I want `-` to be allowed and a `space` and a `+` to be allowed in the code, how should I do that different – Minegolfer May 18 '17 at 14:38
  • ... I actually corrected that type in my initial comment without even realising it! :D ... `[\s0-9+-]{6,12}` < you've got to either escape the hyphen in a block like that, or put it at the end. – CD001 May 18 '17 at 14:39
  • @Wiktor Stribiżew tbh I can't see how its a duplicate with one of those topics. I didn't question about a `unknow modifier` or a string. – Minegolfer May 18 '17 at 14:43
  • Yes, but you need to use regex delimiters. And you would see the warning if you turned them on - see [**this PHP demo**](https://3v4l.org/QZ4VY) showing *Warning: preg_match(): Unknown modifier '{' in ...*. – Wiktor Stribiżew May 18 '17 at 14:44
  • @CD001 thanks for helping it works now, I had a typo what I didnt even see and didn't know about the open and closing `/`. How do I add it so it also accepts a `+` and a `-` ? – Minegolfer May 18 '17 at 14:44
  • @WiktorStribiżew oh it was more ment as answer? I see now :) – Minegolfer May 18 '17 at 14:45
  • Try `preg_match('/[\s()0-9+-]{6,24}/', $tel)` ... I've made it a longer match and included parentheses for things like `+34 (0)123 123-456789`; phone numbers aren't as regular as you'd hope ;) https://regex101.com/r/EyQzc8/1 – CD001 May 18 '17 at 14:47
  • Ye I know but I only wanna get dutch mobile phone numbers :) – Minegolfer May 18 '17 at 14:47

0 Answers0