1

i was using standard preg_match for making url excluding

  http://domainlllll.com/

and it was working without any issue

  preg_match("/^[0-9a-z_\/.\|\-]+$/",$url)

but now i want to support multiple languages so i used this and it is also working without any problem

   preg_match("/\S+/",$url)

my url is

link/kn/some-word-গরম-এবং-সেক্সি-ইমেজ/611766

but i want to exclude some special characters which is hackers favorite like single quotes and other. i dont want to exclude all special character as few are part of few languages and it will break those languages in link

Any guide will be great

swetlana
  • 47
  • 7

1 Answers1

0

Look, the /^[0-9a-z_\/.\|\-]+$/ regex requires the whole string to match the patterns, 1+ chars from the defined ranges (digits, lowercase ASCII letters) and sets. The /\S+/ regex does not require a full string match since there are no anchors, ^ (start of string) and $ (end of string), and matches 1 or more non-whitespace chars anywhere inside a string.

If you plan to match strings that only contain non-whitespace symbols and NOT quotes, use

preg_match('~^[^\s\'"]+$~', $url)

The ^[^\s\'"]+$ matches

  • ^ - start of string
  • [^\s\'"]+ - 1 or more chars other than whitespace (\s), ' and "
  • $ - end of string (better use \z instead if you need to validate a string).
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563