removing strange characters from php string

Try this:

$newtitle = html_entity_decode($newtitle, ENT_QUOTES, "UTF-8")

If this is not the solution browse this page http://us2.php.net/manual/en/function.html-entity-decode.php


This is my function that always works, regardless of encoding:

function RemoveBS($Str) {  
  $StrArr = str_split($Str); $NewStr = '';
  foreach ($StrArr as $Char) {    
    $CharNo = ord($Char);
    if ($CharNo == 163) { $NewStr .= $Char; continue; } // keep £ 
    if ($CharNo > 31 && $CharNo < 127) {
      $NewStr .= $Char;    
    }
  }  
  return $NewStr;
}

How it works:

echo RemoveBS('Hello õhowå åare youÆ?'); // Hello how are you?

This will remove all non-ascii characters / special characters from a string.

//Remove from a single line string
$output = "Likening ‘not-critical’ with";
$output = preg_replace('/[^(\x20-\x7F)]*/','', $output);
echo $output;
 
//Remove from a multi-line string
$output = "Likening ‘not-critical’ with \n Likening ‘not-critical’ with \r Likening ‘not-critical’ with. ' ! -.";
$output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $output);
echo $output;

Tags:

Php