php replace all forms of quotes - php

I have mysqldb with some in on series episodes.
But when i fetch this and echo it i get some strange square with a question mark in it.
I want to replace all these to either nothing or something more suitable.
I have googled my ass of and found solution for double quotes but i would like a function that matches all forms of quotes
here is some example
“senior tanning”
�senior tanning�
Mayor’s
Mayor�s
don't know the names of these in English that is why i posting examples :)
I started with php first time yesterday so please be nice :)

When you echo the contents from the database, make sure you have the charset meta tag set to utf-8.
HTML DOCUMENT
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
And in PHP before any output:
header('Content-Type: text/html; charset=utf-8');
And lastly set the MySQL transfer charset:
PDO:
$db->query("SET NAMES utf8");
Or oldschool:
mysql_query("SET NAMES utf8");
and while you're at it, change the MySQL table collation to UTF8_unicode or UTF8_general
You can even add some magic to .htaccess:
AddDefaultCharset utf-8
I've had these charset problems plenty of times, so I know where to find them :)

Related

Issue with hebrew

I am having issues using hebrew with mysql and PHP, i have already tried this:
the db collation has to be utf8_general_ci.
the collation of the table with hebrew has to be utf8_general_ci
in my php connection script i put
header('Content-Type: text/html; charset=utf-8');
in my xhtml head tag i put
after selecting the db in the connection script i put
mysql_query("SET NAMES 'utf8'");
And it is still not working. In phpmyadmin i can see the hebrew characters but when i display them in php all i see is "?????"
If anyone could help me that would be great!
In your html page
Under <head> tage, add <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
At the time of mysql insert use utf8_encode & when show data try to use utf8_decode

Em Dash and En Dash from MySQL Db with PHP

I'm trying out output a MySQL database field with PHP which contains em dash and en dash, but although the ROWs output, the values with these dashes do not.
As far as I am aware, these are characters which should be used in proper english, therefore I don't think I should be stripping them out or replacing them with an alternative (like a hyphen).
By adding this code before the INSERT, I am able to get the em dash and en dash into the database properly (whereas without this line I saw unwanted characters instead):
mysql_query('SET NAMES utf8');
But, the value won't output. The database table and it's fields are using the utf8_general_ci collation and I've got these lines in my PHP page:
header('Content-type: text/html; charset=utf-8');
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I'm outputting the value like so:
echo nl2br(htmlentities(preg_replace("/[\r\n]+/", "\n\n", $row['someText'])));
If I output the value without formatting, I see this question mark character:
�
Does anyone know how to get around this? Am I forced to replace them with hyphens even though that's grammatically incorrect, or is there a way to output them as they appear in the database?
I added the same MySQL command before retrieving data from the Db and this solved the issue.
mysql_query('SET NAMES utf8'); // Use utf-8
I don't know why this is needed but I'll investigate and see if I can make this a default.

Charset is not working - Spanish characters

This is really driving me crazy. I'm building a spanish website (meaning a lot of latin characters) and I'm using Zend framework.
When I save a á it displays like á . I know is a charset encoding but I dont understand why.
My head charset is <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
My database is utf8_general_ci . I've changed the charset to others and always the same problem.
When I look in my database, what is saved is an á
Any idea why is this happening? Thanks!
Here goes the recipe to get rid of encoding headaches:
Your DB, tables and fields must use collation utf8_unicode_ci.
Be sure your code (HTML, PHP... all) is UTF-8 encoded.
Always use this meta tag: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
This is long:
If you have access to MySQL file configuration my.cnf just add this line:
init-connect='SET NAMES utf8'
This tells MySQL to return results in UTF-8 for each connection.
If you cannot edit my.cnf but you are using PDO, open the connection this way:
$pdo = new PDO($dsn, $usr, $pwd, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));
If you aren't using PDO... start using it, what are you waiting for? Meanwhile you can execute this after each connection if you use ext/MySQLi:
$mysql->set_charset('utf8');
Or this if you use plain old ext/MySQL:
mysql_set_charset('utf8', $connection);
The character á and other characters outside the ASCII map can be mapped with unicode.
As you are stroing the data in a table with UTF-8 charset and characters are storing properly.
I would suggest you that
Before you store data
Before you fetch data
Run the query SET NAMES UTF-8. This will fetch your UTF-8 data with taking care of the characters which are outside of ASCII map.
While outputting the data you make sure that you are forcing/directing browser to use the UTF-8 charset to print the data.

PHP/MySQL Special Characters aren't displayed properly [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Special characters in PHP / MySQL
I have a problem. I have a piece of text in my database (MySQL 5.5.20) with characters like 'é' and " ' " who aren't displaying properly after executing the MySQL query and displaying it with echo($...). With every special character I've inputted in the database, it displays a small question mark inside a diamond. If I look at the text in the database itself, it is an 'é' and " ' ", so I figured the problem isn't MySQL.
One thing I could do is str_replace everything like " ' " --> "'" on input, but then I have to do this for every character there is.
Oh and I already have included
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
and this didn't work.
Hopefully you've all the information to help me, if not just say :) Thanks in advance!
Milaan
You need to have everything in utf-8:
The database field
The database connection (mysql_set_charset('utf8'); in classic mysql, something like $db->exec('SET CHARACTER SET utf8'); in PDO)
The content type (like you have already)
I was using the SQL query SET NAMES utf8 right after the connection to a DB is done successfully for over a years.
But this is not neccessary when You have everything in the same encoding
source files encoding
table columns collations
web page encoding (both in PHP header('Content-Type: text/html; charset=utf-8'); and in <header> <meta name="Content-Type" value="text/html; charset=utf-8" />)
I usually format all the input text with str_replace an replace all uncommon symbols with their &#xxx; equivalent, this is actually useful to prevent injection and bad html rendering
i.e. if someone inputs html tags they'll be active in your page and so on.

Character encoding issue. Single source displays multiple ways on single page

I have a strange character encoding issue.
I have a page where text is pulled from my database and displayed once on the page, once in the title, and once in a jquery ui modal dialog.
The character is an n with a tilde over it. It is from the same field/column/record in the database.
On the page it is fine. In the title it appears as the diamond/question mark. In the dialog it appears as the diamond/question mark. On a previous page, (it is a CMS, so all the headers are the same) it displays, in the page, as the diamond/question mark.
So what am I missing?
Charset and collation set as UTF8 in MYSQL.
Page header is UTF8 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
I even did this in php:
ini_set('default_charset', 'utf-8');
header('Content-Type: text/html; charset=utf-8');
header('Accept-Charset: utf-8');
header('Accept: text/html');
mb_language('uni');
mb_internal_encoding('UTF-8');
iconv_set_encoding("input_encoding", "utf-8");
iconv_set_encoding("internal_encoding", "utf-8");
iconv_set_encoding("output_encoding", "utf-8");
So, I could understand if it wasn't showing right in all places, but differently on one page is messing with my mind.
Thanks for any help.
So, I'm sort of not so bright. I being by saying that first off, adding SET NAMES fixed many problems across the board. So thank you for that. I will be putting this bit into all of my connection scripts from now on:
mysql_query("SET NAMES UTF8");
I found my error was at this line:
echo htmlentities($person_name);
So, once everything was properly dispalying as utf-8, I still got the
Muñoz
because it seems that you can't convert 3 and 4 byte utf8 characters using htmlentities(). See links below.
http://www.php.net/manual/en/function.htmlentities.php#96648
This issue is also discussed at:
http://www.php.net/manual/en/function.htmlentities.php#92105
Thanks for the help all.

Categories