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

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?

"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.

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);

It isn't really one character, and is likely caused by misalignment between content encoding and browser's encoding. Try to set the encoding of your outputted page to what you are using.
e.g. In the section, output:
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
(Adjust UTF-8 to whatever you are using)

I use this one a lot
function cleanStr($value){
$value = str_replace('Â', '', $value);
$value = iconv('UTF-8', 'ASCII//TRANSLIT//IGNORE', $value);
return $value;
}

This is coming from database so the best option will be remove from database using a SQL query like:
UPDATE products SET description = REPLACE(description, 'Â', ' ');

Use Bellow codes
echo "<META http-equiv='Content-Type' content='text/html; charset=UTF-8'>";
echo htmlspecialchars_decode($your_string, ENT_QUOTES);

This problem occurs when using different charset in your web.
To solve this (using utf-8 in the examples):
in the <HEAD> of your page add charset:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
In any form you submit add accept-charset:
<form name="..." method=".." id=".." accept-charset="utf-8">
If you are using php+MySQLi to process your form, you should make sure the database connection is also supporting your charset. Procedural style:
mysqli_set_charset($link, "utf8");
and object oriented style:
$mysqli->set_charset("utf8")

I Actually had to have all of this:
<--!DOCTYPE html-->
<--html lang="en-US"-->
<--head-->
<--meta charset="utf-8"-->
<--meta http-equiv="X-UA-Compatible" content="IE=edge"-->
<--meta name="viewport" content="width=device-width, initial-scale=1"-->
<--meta http-equiv="Content-Type" content="text/html; charset=utf-8/" /-->

To remove â character from string
mysqli_set_charset($con,"utf8");
$price = "₹ 250.00";
$price2 = preg_replace('/[^(\x20-\x7F)]*/','', $price);
Result : 250.00

Related

How to remove unnecessary characters from string?

There is a problem in character when I fetch data from database.
doesn�t
It�s
I have tried to remove it using str_replace function but it's not working.
str_replace('�','',$str);
The encoding is
<meta charset="UTF-8" />
How can i solve these problem?
Try utf8_encode() function.
It will remove that characters
utf8_decode('doesn�t'); // doesnt
this is not good because it removes ' from doesn't
OR
Just update your Meta tag
<meta charset="UTF-8" />
to
<meta charset="ISO-8859-1" />
Output
doesn't
It's
no need to remove those character, Try to use htmlentities() or utfdecode() function to display those properly
This is because character encoding
Just removing the "�"s would give you wrong output. For instance, the string is probably doesn't, removing � from doesn�t gives you doesnt which is wrong.
What you should do is fix your encoding. You are probably saving or retrieving the strings in an encoding different from your code.

Special Characters In Php and MySQL

I've got a problem with some specials characters in PHP. I have a table in mysql (utf8_hungarian_ci) that contains some text with special characters like á, á, Ó, Ö, ö, ü, and I would like to show this text on my page. I've tested:
$text = htmlentities($text); //to convert the simple spec chars
$search = array("& otilde;","&O tilde;","& ucirc;","&U circ;");
$replace = array("& #337;","& #336;","& #369;","& #368;");
$text = str_replace($search, $replace, $text);
echo $text;
But this code works only if $text isn't set from database. If I use this code and my $text is selected from database, it doesn't shows me any text, and if I only use:
echo $text; without htmlentities and replacements
I get characters like this one: �
I know there were some questions about this and I have tried accepted answers, but it still doesn't work, so please help me if you want and if you have time. Thank you anyway. A good day to you all!
Also try setting in your header to use UTF-8 encoding.
In your PHP file, add
header('Content-type: text/html; charset=utf-8');
as well as specifying the encoding to be UTF-8 in your <meta> tag, to ensure that you told the browser. And see if it fixes the issue.
As well as including UTF-8 encoding in your meta tag.
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
...
</head>
Edit:
If you have access to Apache configuration, see if AddDefaultCharset is set to another encoding.
Try using mysql_set_charset() (mysqli_set_charset() if you're using MySQLi).
Try to put this in you html header:
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
(Also, you may need to save your file in "utf-8" file encoding)
.
Secondly, you could use this to try to tranlate-or-remove the disturbing char that always prints out in your case:
$str_out = #iconv("ISO-8859-1", "UTF-8//TRANSLIT//IGNORE", $str_in);
This is a slightly generic answer but please read up this article I wrote on common character-encoding pitfalls in the PHP/MySQL stack and if you still have problems let's try to work through them.
http://webmonkeyuk.wordpress.com/2011/04/23/how-to-avoid-character-encoding-problems-in-php/

PHP Encoding of Special Characters iso-8859-1

My PHP script parses a web site and pulls out an HTML DIV that looks like this (and saves it as a string)
<div id="merchantinfo">The following merchants: Nautica®, Brookstone®, Teds® ©2012 Blabla</div>
I store this as $merchantList (string).
However, when I output the data to the webpage
echo $merchantList
The encoding gets messed up and displays as:
Nautica®, Brookstone®, Teds® ©2012 Blabla
I tried adding the following to the display page:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
But that didn't do anything. --Thanks
EDIT:: ------------
For the question, the accepted answer is correct.
But I realized my actual issue was slightly different.
The initial parsing using DOMDocument::loadHTML had already mangled the UTF-8 encoding, causing the string to save as
<div id="merchantinfo">The following merchants: Nauticaî, Brookstoneî, Tedsî ©2012 Blabla</div>
This was solved by:
$html = mb_convert_encoding($html, 'HTML-ENTITIES', "UTF-8");
$dom->loadHTML($html);
Use:
ini_set('default_charset', 'UTF-8');
And do not use iso-8859-1. Use UTF-8.
From the mojibake you posted the input string is utf-8, not iso-8859-1.
You need just to Use htmlspecialchars_decode function , exemple :
$string = '"hello dude"';
$decodechars = htmlspecialchars_decode($string);
echo $decodechars; // output : "hello dude"

Is there a PHP function converting accentuated letters in database into html code?

There is a MySQL database containing data with accentuated letters like é. I want to display it in my PHP page , but the problem is that there are unrecognized characters displayed because of the accent. So is there a function to convert accent to HTML code , for example é is converted to é !
Rather than using htmlentities you should use the unicode charset in your files, e.g.
<?php
header('Content-Type: text/html; charset="utf-8"');
To be on the safe side, you can add the following meta tag to your html files:
<html>
<head>
<meta charset="utf-8" />
Then, make sure that your data base connection uses utf8:
mysql_connect(...);
mysql_select_database(...);
mysql_set_charset('utf-8');
Then, all browsers should display the special characters correctly.
The advantage is that you can easily use unicode characters everywhere in your php files - for example the copyright sign (©) or a dash (–) - given that your php files are encoded in utf-8, too.
Try htmlspecialchars() and/or htmlentities()
you can easily make one yourself with str_replace:
function txtFormat($input){
$output = str_replace('/\à/','É',$input);
$output = str_replace('/\è/','è',$output);
$output = str_replace('/\é/','é',$output);
$output = str_replace('/\ì/','ì',$output);
$output = str_replace('/\ò/','ò',$output);
$output = str_replace('/\ù/','ù',$output);
return $output;
}
Use the following code
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
And don't encode your data via utf8_encode() function before inserting into database
hope this will solve your problem :)

error using php to print out special charcters from a database

Alright, what I'm trying to do is print out a few lines of text onto a page from a database, and the problem I'm running into is when it sees something like a ' in the text, it puts out a ?. So what I'm wondering is there a way around this? I know with HTML you can use special characters, but I'm working with dynamic data here. I did try doing this, but it's just not working:
<?php
include("connect.php");
$queryCurUser = "SELECT * FROM wn_current;";
$queryResult2 = mysql_query($queryCurUser, $conn) or
die ("queryResult Failed: ". mysql_error());
while ($row = mysql_fetch_array($queryResult2))
{
$body = $row['body'];
echo $row['heading'];
echo"<br/>";
if($body == "'"){
echo"hot dog";
}
echo"<br/>";
echo $row['pdflink'];
}
?>
You need to change the character set used in the database to one with more characters - UTF-8, for instance.
You need to tell the browser what encoding the data is.
Most likely this tag will solve your woes:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
If that doesn't work, try this one:
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
Check the character set that your database table uses; you may want to change it to use UTF-8. Also consider using htmlentities() on your echoed output with the correct charset to convert any characters that have HTML character entity equivalents, e.g.
echo htmlentities($body, ENT_NOQUOTES, 'UTF-8', false);
If you store international characters (á, ê, etc..), not only do you need to choose the correct character encoding for your database (e.g. UTF-8), but you also need to tell the browser to use the correct character set, by way of
a meta tag:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
or a php header:
header("Content-Type: text/html; charset=utf-8");
Also, it may be necessary to specify the charset that the MySQL extension should use with mysql_set_charset().
$body=str_replace['"', """, $body]; // double quotes
seemed to work

Categories