Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > display VARCHAR(mysql) and special chars in html

Reply
Thread Tools

display VARCHAR(mysql) and special chars in html

 
 
Jonas Meurer
Guest
Posts: n/a
 
      02-21-2005
hello,

my script selects a comment saved as VARCHAR in MySQL and displays it
inside an html page.

the problem is, that the comment contains several special characters, as
mysterious utf-8 hyphens, german umlauts, etc.

i could write a function to parse the comment and substitute special
chars with the relevant html code, but maybe this already exists in some
module?

if not, it'll be hard work, as i've to consider many special chars, and
at least iso-8859-1* and utf-8 as charmaps.

bye
jonas
 
Reply With Quote
 
 
 
 
Radovan Garabik
Guest
Posts: n/a
 
      02-22-2005
Jonas Meurer <> wrote:
> hello,
>
> my script selects a comment saved as VARCHAR in MySQL and displays it
> inside an html page.
>
> the problem is, that the comment contains several special characters, as
> mysterious utf-8 hyphens, german umlauts, etc.
>
> i could write a function to parse the comment and substitute special
> chars with the relevant html code, but maybe this already exists in some
> module?


just make the page in utf-8, and you'll save you a lot of troubles


>
> if not, it'll be hard work, as i've to consider many special chars, and
> at least iso-8859-1* and utf-8 as charmaps.


if you insist...
a = u'\u010c'
a.encode('ascii', 'xmlcharrefreplace')


--
-----------------------------------------------------------
| Radovan GarabĂ*k http://melkor.dnp.fmph.uniba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
 
Reply With Quote
 
 
 
 
Jonas Meurer
Guest
Posts: n/a
 
      02-22-2005
On 22/02/2005 Radovan Garabik wrote:
> > i could write a function to parse the comment and substitute special
> > chars with the relevant html code, but maybe this already exists in some
> > module?

>
> just make the page in utf-8, and you'll save you a lot of troubles


ok, how do i do this? simply add a second line with this
# -*- encoding: utf-8 -*-

i use utf8 locales on my machine anyway.

> > if not, it'll be hard work, as i've to consider many special chars, and
> > at least iso-8859-1* and utf-8 as charmaps.

>
> if you insist...
> a = u'\u010c'
> a.encode('ascii', 'xmlcharrefreplace')


this fails as the comment contained several chars that couldn't be
converted.

i've changed my plans, and now will transform the comments to html
before saving them in mysql. this way, the comment never contains
special chars except they weren't filtered out when safed in mysql.

do any filters exist, to transform plain text to html? otherwise i might
use third-party products, as text2html.

what do you think?

bye
jonas
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      02-22-2005
Jonas Meurer wrote:

> On 22/02/2005 Radovan Garabik wrote:
>
>>>i could write a function to parse the comment and substitute special
>>>chars with the relevant html code, but maybe this already exists in some
>>>module?

>>
>>just make the page in utf-8, and you'll save you a lot of troubles

>
>
> ok, how do i do this? simply add a second line with this
> # -*- encoding: utf-8 -*-
>
> i use utf8 locales on my machine anyway.
>
>
>>>if not, it'll be hard work, as i've to consider many special chars, and
>>>at least iso-8859-1* and utf-8 as charmaps.

>>
>>if you insist...
>>a = u'\u010c'
>>a.encode('ascii', 'xmlcharrefreplace')

>
>
> this fails as the comment contained several chars that couldn't be
> converted.
>
> i've changed my plans, and now will transform the comments to html
> before saving them in mysql. this way, the comment never contains
> special chars except they weren't filtered out when safed in mysql.
>
> do any filters exist, to transform plain text to html? otherwise i might
> use third-party products, as text2html.
>
> what do you think?
>

I think you should store your data with a known encoding, then encode it
as necessary for transmission. That way you can provide it in the forms
most relevant to different clients.

regards
Steve

 
Reply With Quote
 
Wolfram Kraus
Guest
Posts: n/a
 
      02-23-2005
Jonas Meurer wrote:
> hello,
>
> my script selects a comment saved as VARCHAR in MySQL and displays it
> inside an html page.
>
> the problem is, that the comment contains several special characters, as
> mysterious utf-8 hyphens, german umlauts, etc.
>
> i could write a function to parse the comment and substitute special
> chars with the relevant html code, but maybe this already exists in some
> module?
>
> if not, it'll be hard work, as i've to consider many special chars, and
> at least iso-8859-1* and utf-8 as charmaps.
>
> bye
> jonas

If I understand you correctly, just put

<meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">

somewhere in the <head>-section of you HTML-Page.

HTH,
Wolfram
 
Reply With Quote
 
Radovan Garabik
Guest
Posts: n/a
 
      02-23-2005
Wolfram Kraus <> wrote:
> Jonas Meurer wrote:
>> hello,
>>
>> my script selects a comment saved as VARCHAR in MySQL and displays it
>> inside an html page.
>>
>> the problem is, that the comment contains several special characters, as
>> mysterious utf-8 hyphens, german umlauts, etc.
>>
>> i could write a function to parse the comment and substitute special
>> chars with the relevant html code, but maybe this already exists in some
>> module?
>>
>> if not, it'll be hard work, as i've to consider many special chars, and
>> at least iso-8859-1* and utf-8 as charmaps.
>>
>> bye
>> jonas

> If I understand you correctly, just put
>
> <meta http-equiv="CONTENT-TYPE" content="text/html; charset=utf-8">
>
> somewhere in the <head>-section of you HTML-Page.


.... and make sure the deault charset of your HTTP server is *OFF* (or
UTF-, since it overrides the per-page setting (most unfortunate).

--
-----------------------------------------------------------
| Radovan GarabĂ*k http://melkor.dnp.fmph.uniba.sk/~garabik/ |
| __..--^^^--..__ garabik @ kassiopeia.juls.savba.sk |
-----------------------------------------------------------
Antivirus alert: file .signature infected by signature virus.
Hi! I'm a signature virus! Copy me into your signature file to help me spread!
 
Reply With Quote
 
deelan
Guest
Posts: n/a
 
      02-23-2005
Jonas Meurer wrote:
(...)
> i've changed my plans, and now will transform the comments to html
> before saving them in mysql. this way, the comment never contains
> special chars except they weren't filtered out when safed in mysql.
>
> do any filters exist, to transform plain text to html? otherwise i might
> use third-party products, as text2html.
>
> what do you think?


as you may known mysql 4.1 offers utf-8 support. il would be
wise to keep everything as utf-8: db, html generation and finally
serve, with correct HTTP headers, pages encoded as utf-8.

to do this you might have to fiddle with mysql settings and make
sure that issuing a:

show varibles;

almost all of these settings:

character_set_client latin1
character_set_connection latin1

character_set_database latin1

character_set_results latin1
character_set_server latin1
character_set_system utf8


use utf-8 (as you can see my copy of mysql does not), otherwise i
think bad things will occur.

if you prefer to filter out weird characters and
encode as html &#xxxx entities textile[1] does the job
just fine, you can specify input and output encoding.

cheers,
deelan.

[1] http://dealmeida.net/en/Projects/PyTextile/

--
@prefix foaf: <http://xmlns.com/foaf/0.1/> .
<#me> a foaferson ; foaf:nick "deelan" ;
foaf:weblog <http://blog.deelan.com/> .
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
Problem with AJAX and Special Chars (Perl, MySQL 4) design4future Perl Misc 1 03-16-2006 05:54 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
Problem with minidom and special chars in HTML Horst Gutmann Python 6 02-24-2005 11:38 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57