Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is this character ( Â ) and how do I remove it with PHP?

Tags:

It's a capital A with a ^ on top: Â

It is showing up in strings pulled from webpages. It shows up where there was previously an empty space in the original string on the original site. This is the actual character that is stored in my database. It's also what displays on my website when I echo a string that contains it.

I realize it's a character encoding problem when I originally process the webpage, but I am now stuck with these characters in my database. I have to convert this character when it is displayed, or somewhere else in the php before outputting html that contains it. I cannot reprocess the original documents.

I have tried str_replace() and html_entity_decode() and neither do anything.

What else should I try?

like image 820
T. Brian Jones Avatar asked Aug 25 '11 07:08

T. Brian Jones


People also ask

What is this character â?

Â, â (a-circumflex) is a letter of the Inari Sami, Skolt Sami, Romanian, and Vietnamese alphabets. This letter also appears in French, Friulian, Frisian, Portuguese, Turkish, Walloon, and Welsh languages as a variant of the letter "a".

How can I remove one character from a string in PHP?

In PHP to remove characters from beginning we can use ltrim but in that we have to define what we want to remove from a string i.e. removing characters are to be known. $str = "geeks" ; // Or we can write ltrim($str, $str[0]); $str = ltrim( $str , 'g' );

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 do I remove a specific character from a string?

You can also remove a specified character or substring from a string by calling the String. Replace(String, String) method and specifying an empty string (String. Empty) as the replacement.


2 Answers

"Latin 1" is your problem here. There are approx 65256 UTF-8 characters available to a web page which you cannot store in a Latin-1 code page.

For your immediate problem you should be able to

$clean = str_replace(chr(194)," ",$dirty) 

However I would switch your database to use utf-8 ASAP as the problem will almost certainly reoccur.

like image 179
James Anderson Avatar answered Oct 12 '22 00:10

James Anderson


This works for me:

$string = "Sentence ‘not-critical’ and \n sorting ‘not-critical’ or this \r and some ‘not-critical’ more. ' ! -."; $output = preg_replace('/[^(\x20-\x7F)\x0A\x0D]*/','', $string); 
like image 41
Grant Avatar answered Oct 12 '22 02:10

Grant