0

So I just want to make sure I've wrapped my head around this correctly, if I'm wrong or perhaps not understanding something to it's fullest potential please let me know!

if (!preg_match('/^[0-9]+$/', $ADD) || !preg_match('/^[a-zA-Z]+$/i', $ADD) {
      array_push($errors, " Invalid address.");
}

$ADD = preg_replace("/^[a-zA-z0-9\s-#\/]+$/i", "", $ADD);

So:

  • ^ = Match the expression from the start of the string ...
  • $ = ... to the end of the string
  • + = Look for one or more instance of this. (Does this mean there HAS to be one or more instance?)
  • /i = This is case sensitive search. In theory if i'm using this, do I really need to check for a-zA-z or could I just use /a-z/i ?
  • \s = This checks for any amount of white spaces.

Do I even need to add in ^ and $ to check the entire string start to fin, or will it just do so without ^$ included?

SE_net4 the downvoter
  • 21,043
  • 11
  • 69
  • 107
Pipes
  • 61
  • 4
  • 4
    I would suggest going to a site like https://regex101.com and playing with your regex and some sample input data. Also read [this Q&A](https://stackoverflow.com/questions/22937618/reference-what-does-this-regex-mean) – Nick May 15 '21 at 03:05
  • Yes, you can use `/[a-z]/i` instead of `/[a-zA-Z]/` but pay attention, you wrote `a-zA-z`. The last z is a lower `z` in your pattern. [`A-z` would even match some special characters](https://stackoverflow.com/questions/4923380/difference-between-regex-a-z-and-a-za-z). – bobble bubble May 15 '21 at 10:18

0 Answers0