473,382 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

question about php script handling/displaying UTF8 email

Hi,

I'm almost done with a php driven email filter and automated forwarder,
I've tested it out with various emails and ironed out plain text and
html.
But this final item has me stumped.

When processing an email which contains UTF8 encoded characters, I
can't work out how to detect the presence of the UTF8 characters, so I
get =E2=80=99 displayed instead of a '.
And when forwarded, the =E2=80=99 is sent as plain text losing the
information that it is a utf8 encoded character as opposed to plain
text.

This is the last item I need to resolve.

I tried looking up/searching on:
utf8_decode
utf8_encode
mb_convert_encoding (actually I get function not found when I try it in
the script)
imap_utf8

but can't work it out.
I want to do either of the following
1) convert the utf8 encoded characters to closest local characters or
html codes
or
2) inject headers when the email is forwarded so that the recipents
email client will recognise correctly the presence of the utf8
characters.

Also, what is the easiest way to detect the presence of the utf8
characters, is there a header record I can search for?

Any help would be appreciated as this is furstrating me.

Chris.

Jan 23 '06 #1
4 2395
followup,

I enabled the mbstring library in my php.ini, so I could test with the
mb_convert_encoding function, but that still leaves utf8 encoded
characters in the text body.

I tried
$body = mb_convert_encoding($body, 'HTML-ENTITIES', "UTF-8");
based on an example at www.php.net, but to no avail.

Any ideas?

Chris

Jan 23 '06 #2
ch**************@hotmail.com wrote:
Hi,

I'm almost done with a php driven email filter and automated forwarder,
I've tested it out with various emails and ironed out plain text and
html.
But this final item has me stumped.

When processing an email which contains UTF8 encoded characters, I
can't work out how to detect the presence of the UTF8 characters, so I
get =E2=80=99 displayed instead of a '.
And when forwarded, the =E2=80=99 is sent as plain text losing the
information that it is a utf8 encoded character as opposed to plain
text.


Here's a function I use for utf8 detection (I think I grabbed it from
the manual somewhere):

/************************************************** ***********/
/* Returns TRUE if a string is UTF-8. */
/* Returns FALSE if a string is not UTF-8. */
/* Compatible with 31-bit encoding scheme of Unicode 3.x */
/************************************************** ***********/

function seems_utf8 ($Str) {
for ($i=0; $i<strlen($Str); $i++) {
if (ord($Str[$i]) < 0x80) continue; # 0bbbbbbb
elseif ((ord($Str[$i]) & 0xE0) == 0xC0) $n=1; # 110bbbbb
elseif ((ord($Str[$i]) & 0xF0) == 0xE0) $n=2; # 1110bbbb
elseif ((ord($Str[$i]) & 0xF8) == 0xF0) $n=3; # 11110bbb
elseif ((ord($Str[$i]) & 0xFC) == 0xF8) $n=4; # 111110bb
elseif ((ord($Str[$i]) & 0xFE) == 0xFC) $n=5; # 1111110b
else return false; # Does not match any model
for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
if ((++$i == strlen($Str)) || ((ord($Str[$i]) & 0xC0) != 0x80))
return false;
}
}
return true;
}

Then to decode:

/************************************************** ***********/
/* utf8_encode will encode a string that is already encoded! */
/* This means that everytime you utf8_encode a string it */
/* will grow and grow exponentially! */
/* Use this function instead of utf8_encode to check if a */
/* string is already encoded before encoding. */
/* Needs the seems_utf8 function. */
/************************************************** ***********/

function utf8_ensure ($str) {
return seems_utf8($str)? $str: utf8_encode($str);
}

--
Andrew @ Rockface
np: (Winamp is not active ;-)
www.rockface-records.co.uk
Jan 24 '06 #3
I have a similar problem:

I have a form where I didn't know what kind of languages is used by
visitor. I think the best way is to convert everything to utf-8 (if you
can). In my case this is the best solution, also if you sore the data
inside mysql.

Just use overall utf-8 encoding (forms, pages, email scripts)

--

gr. Olaf
PHP classes and scripts: http://www.finalwebsites.com

Jan 24 '06 #4
Yes I am storing it in a MySQL database,
I wonder if it would be safer just to store the body text in the DB as
a 'blob', cos I wonder just how messed up the text gets if trying to
store utf-8 as a text field.

I'll try Andrew's detection/encode functions as well as using a blob
field and report back.

Thanks,
Chris

Jan 24 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: lkrubner | last post by:
Last year I asked a bunch of questions about character encoding on this newsgroup. All the answers came down to using ord() in creative ways to try to make guesses about multi-byte characters. I...
6
by: Paul Winkler | last post by:
This is driving me up the wall... any help would be MUCH appreciated. I have a module that I've whittled down into a 65-line script in an attempt to isolate the cause of the problem. (Real...
5
by: Tim Morrison | last post by:
Is there any easy way to create a change script as illustrated below for all tables within a database? Right now I would have to create a seperate script for each table. I would like to be able...
3
by: hunterb | last post by:
I have a file which has no BOM and contains mostly single byte chars. There are numerous double byte chars (Japanese) which appear throughout. I need to take the resulting Unicode and store it in a...
4
by: uday.sen | last post by:
Hi, I need to convert a string from UTF8 to wide character (wchar_t *). I perform the same in windows using: MultiByteToWideChar(CP_UTF8, 0, pInput, -1, pOutput, nLen); However, in linux...
9
by: eholz1 | last post by:
Hello PHP group, I looks like this is a good forum to ask my question. I am using both PHP and MySQL. I would like/am designing a photo database for the web - not heavy duty - not complex,...
4
by: EmeraldShield | last post by:
(Dot Net 2 C# application - using Encoding.UTF8 with a StreamReader) I have a very strange problem that I cannot explain with a UTF8 Readline() although this could exist in other types of encoding,...
7
by: amygdala | last post by:
Hi, I'm trying to let PHP write a 'sitemap.xml' sitemap for Google and other searchengines. It's working, except that the content in the XML file doesn't seem to be UTF8. (Which it should be,...
5
by: Robert Latest | last post by:
Hello, I'm new to Python but have lots of programming experience in C, C++ and Perl. Browsing through the docs, the email handling modules caught my eye because I'd always wanted to write a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.