By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,931 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,931 IT Pros & Developers. It's quick & easy.

In the opinion of PHP, what is a character?

P: n/a

I'm worried about idiot users that write long essays in Microsoft Word,
then log into their accounts and bring up an HTML form and copy and
paste the essay and hit submit. Or perhaps they do this using
WordPerfect. Or perhaps they use MacWrite.

I output everything from my site as UTF-8. I'd like to check the input
for characters that are not UTF-8 and then turn the bad ones to an
ASCII question mark. I could loop through a string as if it was an
array and test each character, but what does PHP think a character is?
Does PHP understand what a multi-byte character is? Would this work?
// the $string is a form input, possibly containing characters
// written in any of the world's word processors
$finalString = "";
for ($i=0; $i < strlen($string); $i++) {
$char = $string[$i];
$encoding = mb_detect_encoding($char);
if ($encoding != "UTF-8") {
$char = "?";
}
$finalString .= $char;
}

They offer this on www.php.net, in the comments, but, again, I'm not
sure it would work on individual characters, and I'm about reading
Regx.

===========================

Much simpler UTF-8-ness checker using a regular expression created by
the W3C:

<?php

// Returns true if $string is valid UTF-8 and false otherwise.
function is_utf8($string) {

// From http://w3.org/International/question...rms-utf-8.html
return preg_match('%^(?:
[\x09\x0A\x0D\x20-\x7E] # ASCII
| [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
| \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
| [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
| \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
| \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
| [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
| \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
)*$%xs', $string);

} // function is_utf8

?>

Jul 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<lk******@geocities.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
what does PHP think a character is?
Does PHP understand what a multi-byte character is?


The user's manual is your friend here...

"In PHP, a character is the same as a byte, that is, there are exactly 256
different characters possible. This also implies that PHP has no native
support of Unicode. See utf8_encode() and utf8_decode() for some Unicode
support."

quoted from

http://us2.php.net/manual/en/language.types.string.php
Jul 21 '05 #2

P: n/a
On 21 Jul 2005 00:29:54 -0700, lk******@geocities.com wrote:
I output everything from my site as UTF-8. I'd like to check the input
for characters that are not UTF-8 and then turn the bad ones to an
ASCII question mark.
Déja vu all over again. ;-p
I could loop through a string as if it was an
array and test each character, but what does PHP think a character is?
PHP's string data type has no knowledge of character encodings. It treats
strings as a meaning-free series of bytes. Not characters.
Does PHP understand what a multi-byte character is?
No. The mbstring extension does, though.
Would this work?

// the $string is a form input, possibly containing characters
// written in any of the world's word processors
$finalString = "";
for ($i=0; $i < strlen($string); $i++) {
$char = $string[$i];
$encoding = mb_detect_encoding($char);
if ($encoding != "UTF-8") {
$char = "?";
}
$finalString .= $char;
}


No. This goes byte-by-byte. There's no reason why mb_detect_encoding should
return UTF-8, since for anything <127 then it could equally be ASCII, or for
other values some other encoding such as ISO-8859-15.

To find invalid UTF-8 encoded byte sequences you have to consider more than
one byte at a time.

As I believe was covered the previous times you've asked this:

You can tell whether a series of bytes is not a series of UTF-8 encoded
characters, by looking for byte sequences that are not valid UTF-8 - look for
lead bytes and the corresponding numbers of trail bytes.

Therefore, your current request (replace byte sequences that cannot be UTF-8
encodings with a "?" character) is quite possible, but you need to consider
more than one byte at a time and will probably have to backtrack a bit if you
get an invalid sequence.

In one previous incarnation of this thread I posted a script to detect invalid
UTF-8 byte sequences; looks like this could be quite easily adapted to your
current request:

http://groups.google.co.uk/group/com...0075dcf?hl=en&

Just remove the various returns that exit when it finds bad characters, and
instead, whenever $charSize drops to zero, append it to a string for output, or
if it finds a bad encoing, append a "?".

However, you cannot tell whether a byte sequence is actually a series of UTF-8
characters, because it could be encoded in something else that happens to share
the same byte representation.

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 21 '05 #3

P: n/a
>>I output everything from my site as UTF-8. I'd like to check the input
for characters that are not UTF-8 and then turn the bad ones to an
ASCII question mark.


Déja vu all over again. ;-p


I know. Every 3 or 4 months I come back to the issue and try to fix it,
but I never get it fixed. This has been going on for 2 years now. Maybe
I'll get it this time.

Jul 21 '05 #4

P: n/a
This should work as well:

preg_match('/./u', $text);

Badly encoded UTF-8 text would cause PCRE to go poopoo and return false
even where the string isn't empty.

Jul 21 '05 #5

P: n/a
lk******@geocities.com wrote:
I output everything from my site as UTF-8. I'd like to check the input
for characters that are not UTF-8 and then turn the bad ones to an
ASCII question mark. [...]


This function simply drop all the illegal sequences and return a legal
UTF-8 string:

function Force_UTF_8($s)
{
return mb_convert_encoding($s, 'UTF-8', 'UTF-8');
}

For example:

Force_UTF_8("A\x00B\xc0\x80C") ==> "A\000BC"

Note that the control characters (here \000) aren't removed.

Should be enought for many cases. You may send a warning to the user if
the resulting string differ from the original one.

Regards,
___
/_|_\ Umberto Salsi
\/_\/ www.icosaedro.it

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.