FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Detecting accents





martindecorte
Hi everyone.

I'm building a website which is supposed to be visited by users from many countries (especially France & Hungary). This website has dynamically addable content (news), to be added in a MySQL database.

My problem is the accented characters. When an user writes an accented letter, i'd like to convert it automatically to the HTML equivalent. For example, if someone writes "thtre", i'd like to save "théâtre" in the database ; then the clients won't have any visual problem when browsing my website.

I wrote a converting function, which looks like this:
Code:
function htmlize ($msg)
{
$output = '';
for ($i=0;$i<strlen($msg);$i++)
{
switch($msg[$i])
{
case '': $output = $output.'&aacute;'; break;
(... many other cases ...)
case '': $output = $output.'&ccedil;'; break;
default: $output = $output . $msg[$i]; break;
}
}
return $output;
}


But this doesn't always work. I couldn't figure out exactly in which cases it doesn't work, but many users are already adding news in my website, and I found accented letters in my database, that weren't converted by my function, while they should have been. Some accented letters were correctly converted, though.

Does anyone know why my method doesn't work correctly, or could anyone give me another method ?

Thanks already.
mathiaus
I would use preg_replace ( http://uk.php.net/manual/en/function.preg-replace.php )

The code would look something like
Code:
$search = array ('', '');
$replace = array ('&aacute;', '&ccedil;');
$output = preg_replace($search, $replace, $msg);

just keep adding to the arrays as much as you like
snowboardalliance
I think http://us3.php.net/manual/en/function.htmlentities.php

does that, I'm not sure though.
Atomo64
mathiaus wrote:
I would use preg_replace ( http://uk.php.net/manual/en/function.preg-replace.php )

The code would look something like
Code:
$search = array ('', '');
$replace = array ('&aacute;', '&ccedil;');
$output = preg_replace($search, $replace, $msg);

just keep adding to the arrays as much as you like

I would better use strtr, example:

Code:

$replacements = array ('' => '&aacute;', '' => '&ccedil;');
$output = strtr($msg, $replacements);


if it is something that doesn't need regular expressions, then don't use it... but maybe you could even use str_replace instead of strtr
Related topics
i have question
What is the best antivirus progrem?
hola ! learning spanish
Microsoft is upto it's dirty games again!
accents in my phpbb forum?
Frankfurt Motor Show, 2005
Which Linux do you use?
Webpage Design Programs
F.E.A.R
Birthday alert
The Wi-Fi Detector Ring
Fedora Core 5 Test 1 Review
Detecting Nerve Signals
English accents in American movies and TV shows
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.