简体   繁体   中英

Check string for high ascii, punctuation, other weird characters

I've got a string called $ID coming in from a different page and hitting base64_decode($enc); and want to check it for any weird characters. $ID when decrypted should only contain letters, numbers, underscores and dashes.

I've had a bit of a look at preg_replace('/[\\x80-\\xFF]/', '', $string); which cuts out some weird characters---which is helpful---but I can still see sometimes that @ signs and brackets and stuff still make it in.

Is there a way I can lower the ascii test? Or how else do I cut out everything except letters, numbers, underscores and dashes?

Any help at pointing me in the right direction is wonderful and thanks!

$enc = $_GET["key"];
$ID= base64_decode($enc);

if (empty($enc)) { echo "key is empty"; } else {

    echo "string ok<br>"; 

    $check = preg_replace('/[\x80-\xFF]/', '', $ID);
    echo $check;
    // i can see this step is helping cut junk out, do more tests from here
 }

在左方括号后键入插入符号会否定字符类,因此您可以执行以下操作:

$check = preg_replace('/[^A-Za-z0-9_-]/', '', $ID);

You can use this replacement:

$check = preg_replace('~[^[:word:]-]+~', '', $ID);

The [:word:] character class contains letters, digits and the underscore.

To make the string lowercase, use strtolower()

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