473,766 Members | 2,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_enco ding (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 2416
followup,

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

I tried
$body = mb_convert_enco ding($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($st r);
}

--
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
1912
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 was a little amazed at this and wondered if I'd somehow misunderstood the situation. I'm pleased to find that Joel Spolsky shared my amazement and offered some criticism of PHP on these grounds: "When I discovered that the popular web development...
6
2832
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 domain names have been removed in everything below.) SYNOPSIS: I have 2 target servers, at https://A.com and https://B.com. I have 2 clients, wget and my python script.
5
2628
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 to update the customers database while preserving their exisiting data. -------------------------------------------------------------------------------- BEGIN TRANSACTION
3
7773
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 DB and display it onscreen. No matter which way I open the file, convert it to Unicode/leave it as is or what ever, I see all single bytes ok, but double bytes become 2 seperate single bytes. Surely there is an easy way to convert these mixed...
4
22050
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 this API is not available. However, there exists mbstowcs() API, which converts multibyte string to wide character. But will this API convert UTF8 encoded string to wide character? Or this
9
1365
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, like menalto Gallery, etc. My idea is to use PHP and MySQL. I would like to store the images in the MySQL database, and display them using PHP. I would like to have a table with thumbnail images, that when "clicked" will link to a veiw page that...
4
4892
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, I have not tried them. Our application wrote this sequence to a UTF8 file. Now I am loading it back and the text is not coming back in the same as it went out. DATA: from: processfrom checkemail failed: 501 syntax error in parameters:...
7
13108
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, judging by the information given on Google's webmaster helpcenter). The way I test to see if the content is UTF8, is by opening the XML file in notepad and choose 'save as...'. Normally the coding option should be set to UTF8, but now it just...
5
1272
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 script to handle my huge, ancient, and partially corrupted email archives. Of course I know that this kind of project shouldn't be tackled by a beginner in a language, but I still thought I'd give it a spin.
0
9404
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10168
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9837
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8833
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7381
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6651
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3929
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3532
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2806
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.