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
Related
fm API to get event discription, venue name etc...
Now sometimes I get special chars back like: ' é à , but they show up scrabled.
So how can I display them properly? Also with the descrioption I get html-tags back, but I do want to keep these.
Can someone help me out fot those both cases? The language I'm using is php
Thanks in advance
specify encoding in the header:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
...
encode string when handling the input
$str=utf8_encode($str);
if you are displaying the input back as-is, no encoding is required;
however, if the value is the content of an input or textarea, escape the html characters
<?php echo htmlspecialchars($str); ?>
For Latin characters, use
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
in your section.
you need to be sure about two things, the meta header referring to which enconding you will be using, and the encoding you are using for the text served.
If you are using a utf8 header just be sure to convert the text served to utf8, refer to functions for encoding conversion like : mb_convert_encoding
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/
How can I vanish out the odd string like � in php?
I already use like html_entity_decode php function but it's still not works.
Try to add these line in your db config file.
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_set_charset('utf8',$con);
mysql_select_db("db_name", $con);
<meta http-equiv="Content-Type" content="text/html; charset=utf8" />
� isn't just one symbol -- it's the symbol used to replace a symbol that your browser can't make sense of. Usually this is caused by encoding issues -- you set the wrong encoding, or someone is using a symbol from a different encoding.
You can't just magically strip it out, because it isn't just one character. Simply put, encoding is one place where you can't just wave a magic wand -- you have to decide on an encoding, and then enforce it.
If you're taking user input, I honestly don't have any clue how to help with the issue. You can check for common causes (curly quotes and dashes as distinct from hyphens are where I run into this the most), and force a manual replace on them, but if a user wants to enter in a badly-encoded character, there's not a lot you can do.
Try changing your <meta charset to iso-8859-1 like
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
If your content is in ANSI encoding use
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
you can check the encoding of your files using a editor like ( NOTEPAD ++ )
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 :)
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