Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing strange characters from php string

Tags:

php

this is what i have right now

Drawing an RSS feed into the php, the raw xml from the rss feed reads:

Paul’s Confidence 

The php that i have so far is this.

$newtitle = $item->title; $newtitle = utf8_decode($newtitle); 

The above returns;

Paul?s Confidence 

If i remove the utf_decode, i get this

Paul’s Confidence 

When i try a str_replace;

$newtitle = str_replace("”", "", $newtitle); 

It doesnt work, i get;

Paul’s Confidence 

Any thoughts?

like image 327
mrpatg Avatar asked Jul 27 '09 15:07

mrpatg


People also ask

How remove all special characters from a string in PHP?

Using str_replace() Method: The str_replace() method is used to remove all the special characters from the given string str by replacing these characters with the white space (” “).

How can I remove all characters from a specific character in PHP?

The substr() and strpos() function is used to remove portion of string after certain character. strpos() function: This function is used to find the first occurrence position of a string inside another string.

How do I remove all special characters from a string in laravel?

This should do what you're looking for: function clean($string) { $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens. return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars. } Hope it helpss!!


2 Answers

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? 
like image 152
David D Avatar answered Sep 22 '22 15:09

David D


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

like image 24
czuk Avatar answered Sep 21 '22 15:09

czuk