Forum Moderators: coopster

Message Too Old, No Replies

preg replace + utf8

preg_replace utf8 accents

         

miguelvidal

9:31 pm on Feb 2, 2010 (gmt 0)

10+ Year Member



hello!

this is my code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>My Page</title>
</head>
<body>
<?php
$texto = 'Descrição do Meu Material de Teste á é í ó ú!?';
$texto = preg_replace( '/[^[:alnum:]!?,. ]/', '', $texto);
echo $texto;
?>
</body>
</html>


an this is the output:
Descri&#65533;&#65533;o do Meu Material de Teste &#65533; &#65533; &#65533; ó ú!?


what can I do to get the correct output WITH the brazilian characters (ç, accents, etc)?

thank you very much,

Miguel Vidal

Readie

11:07 pm on Feb 2, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to webmaster world! (Been wanting an oppurtunity to say that for a while :P)

Took me a while to find this again. I posted this script a while ago:

[webmasterworld.com...]

Should hopefully solve your problem.

miguelvidal

11:58 pm on Feb 2, 2010 (gmt 0)

10+ Year Member



thank you very much! =]

I will verify it... =]

my DB already are utf-8-general-cy

but I will answer you about it soon... =]

thanks in advance,

Miguel Koscianski Vidal

miguelvidal

1:40 am on Feb 3, 2010 (gmt 0)

10+ Year Member



well...
here we go... =]

with a little change, here are the results...
original:
$texto = preg_replace( '/[^[:alnum:]!?,. ]/', '', $texto);

change:
$texto = preg_replace( '/[^[:alnum:]!?,. ]/[b]u[/b]', '', $texto);

output:
Descrição do Meu Material de Teste á é í ó ú!?


so, the u have the power to force 'utf-8' encoding...
BUT I get this output only in local tests... when I send it to the FTP server, the following occurs:
[Descrio do Meu Material de Teste  ]

(notice the whitespaces!)

So... I am back to the start... =/

Doc: [perldoc.perl.org...]

The error occurs with:
\p{IsAlnum}
[:alnum:]

nothing solves my problem...

I didn't know what to do now =/

is your code the best solution possible?
or just one 'turn around'? =/

Thank you very much! =D

Miguel Vidal

Readie

3:09 am on Feb 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My coding will work - There are other solutions, but getting something working is always of higher priority to fine tuning in my opinion.

If you take the code from that post, and save it in the same folder of your server as the file you are trying to do this preg_replace on, and name it:
symbolreplace.php


then have the following code (modified from the example you posted above)

<?php
include 'symbolreplace.php';
$texto = 'Descrição do Meu Material de Teste á é í ó ú!?';
$texto = preg_replace($symb, $repl, $texto);
echo $texto;
?>


It should give you your desired output.