Jump to content

Character Whitelist


chrisprse

Recommended Posts

Hi there

 

I am currently building a text message [sMS] application and I accept a user's input from a textarea.

 

This input will be submitted to the text message [sMS] gateway.

 

I can only allow the following characters:

 

@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^}{\[~]|€

 

If the string contains ANY OTHER character, such as the copyright symbol, etc it should return false.

 

I am running into trouble with the following function:

 

function illegal_characters_check($string) {
return !preg_match('#[^' . preg_quote('@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&\'()*+,-./:;<=>?¡ÄÖÑܧ¿äöñüà^}{\[~]|€', '#') . 'a-z0-9]#i', $string);
}

 

I ran into a problem earlier when trying to calculate the string length. I couldn't use strlen() so I had to use mb_strlen() instead.

 

I don't believe it's performing the check correct because of utf-8/unicode/multi-byte characters. I don't understand the encoding side of things.

 

I have seen mb_ereg() but I wouldn't know how to modify the above function.

 

Is anyone able to shed some light as to why the function is not returning FALSE if it contains any other character apart from the ones allowed?

 

Many thanks in advance - I've spent 10 solid hours on this and I'm pulling my hair out!

 

Regards

Chris

Link to comment
Share on other sites

<?php
$max_length = 240;
$allowed = <<<ALLOWED
@£$¥èéùìòÇØøÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !"#¤%&'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà^}{\[~]|€
ALLOWED;
$allowed = preg_quote($allowed);
if (preg_match("/^[".$allowed."]{0,".$max_length."}$/i",$text_msg)) {
//go
} else {
//fail
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.