Pergunta

I have many descriptions with special characters like é, ô and many more and have tried to remove them with:

$string = str_replace("é", " ", $string)
$string = ereg_replace("é", " ", $string)
$string = mysql_real_escape_string($string)

But nothing works, the special characters are still there and the description is not inserted in the database. I can't remove all special characters because in the description there are html tags that are needed.

Thank you for any help!

Foi útil?

Solução

Easy peasy:

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}

Usage:

echo clean('a|"bc!@£de^&$f g');

Will output: abcdef-g

Edit:

Hey, just a quick question, how can I prevent multiple hyphens from being next to each other? and have them replaced with just 1? Thanks in advance!

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

   return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

refer this link

Outras dicas

http://www.php.net/manual/en/function.str-replace.php

This function returns a string or an array with the replaced values.

You need to use returned value

$string = str_replace("é", " ", $string);

Use character transliteration.

Example:

<?php
$string = "ʿABBĀSĀBĀD";

echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string);
// output: ABBASABAD

?>
mysql_real_escape_string(htmlentities($string));
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top