4

My task was to create a PHP file that will check a name coming from HTML GET and the basis are the following:

  • It can be from a-z in small or capital letters
  • 25 or less characters length
  • Also add the TM Trademark as an exception

I don't know whether I will use REGEX or STRIPOS for this. My professor is asking me to write this in PHP. Some example scenarios:

1. AdriaN = allow
2. $tackoverFlow = not allowed
3. StackoverFl0w = allowed
4. ™ Stack Over Flow ™ = Allowed
5. StækOverFlow = not allowed

Here's what I have come up with so far, can this be improved upon?

 $pattern = '/[a-zA-Z0-9\$\™]{1,24}$/';
HamZa
  • 14,671
  • 11
  • 54
  • 75

1 Answers1

1

As your samples, you've some spaces inside that are allowed. So I suggest this regex:

/^[a-z\d™][a-z\d\s™]{1,24}(?<!\s)$/im

m multi-line and i case-insensitive modifiers are set

If Professor asks for (?<!\s), reply back it's a negative lookbehind to ensure that there's no trailing spaces at the end.

Live demo

revo
  • 47,783
  • 14
  • 74
  • 117