简体   繁体   中英

preg_match condition problem

I wan't to check if a string ($nick_2) got " or ñ

Is this correct? i can't make it work

if ( (strlen($nick_2) >= 3) && (strlen($nick_2) <= 25) && (!preg_match("/\"/", $nick_2)) && (!preg_match("/ñ/", strtolower($nick_2))) ) {

For finding single characters, regexes are massive overkill. Just use

if ((strpos('"', $nick_2) !== FALSE) || (strpos('ñ', $nick_2) !== FALSE)) {
   ... chars were found
}

Possibly your string is in UTF-8, in which case, you must use the u modifier in preg_match and should submit your expression to that function also in UTF-8.

If that's the case, you will also want to do some of these things:

  • Replace strtolower and strlen with mb_ alternatives.
  • Normalize the input.
  • Check if the graphemes where those characters are don't have more code points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM