简体   繁体   中英

Detect special chars in PHP

How can I detect special chars in PHP. I have error when I try to save string to database:

1366 Incorrect string value mysql

So i want to detect this before I try to inster this string to database. How can I detect this type of string? I dont want to change database to utf8mb4. I need detect before insert in php side and skip this string.

I have tried a lot of ways to do this for example mb_detect_encoding return me that this string is UTF-8. I found this function:

public function contains_invalid_characters($text) {
    return (bool) preg_match('/[a-zA-Z0-9äöüß "!§$%&\/()=[\]\?.:,;\-_]/u', $text);
}

And it works but this detect me a lot of strings from languages around the world which I want to have in database like

олей,عيناها or عين

I need to detect only strings which looks like icons or something I dont know how can I named it. Any help would be great

Even i had the same problem, this is the solution that i found on web. You can use this function to detect these special chars.

function has_emojis( $string ) {

    preg_match( '/[\x{1F600}-\x{1F64F}]/u', $string, $matches_emo );

    return !empty( $matches_emo[0] ) ? true : false;

}

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