Charecter encoding in PHP not working for double quotes - php

I have a PHP script which reads some input and saved it in the database. The input could have single or double quotes. I am changing the quotes to their ISO Latin-1 codes to avoid problems with escaping. For single quotes, my code is:
$str = str_replace("'", "'", $str);
With this code, if the input is "Jack's book", it is being correctly saved in the database as Jack's (code in place of ') book. But when I do this with double quotes:
$str = str_replace("\"", """, $str);
nothing gets saved in the database at all. Can anyone tell me why?

Did you try
$str = str_replace('"', """, $str);
to do the job? (double-quote in single-quotes)

Related

Create variable with apostrophe and double quote PHP

How to create variable when you have massive data with ' and " in it whitout addslashes.
<?
// Create variable with single and double quote.
s = ' massive data with """ and '''' inside.... ';
?>
I try
addslashes(s) = ' massive data in with """ and '''' inside.... ';
but i think i miss something. I cant change manually every apostrophe because its massive.
Thanks you !
You're looking for the backslash escape character:
$string = ' massive data in with """ and \'\'\'\' inside....';
Simply use a \ before any character you wish to be interpreted as part of a string.
I've created an 3v4l demonstrating the above code here.
Alertnatively, if you have a large text string that contains both single quotes and double quotes, you can use a heredoc to avoid needing to escape individual characters:
$str = <<<EOD
' massive data with """ and '''' inside.... ';
EOD;
echo $str;
I've created another 3v4l example of this here.
Hope this helps! :)

how to replace apostrophe (’) with single quote(')

It seems there are two types of apostrophes.I want to know the difference between this character(’) and this(')
the first one was copied from Microsoft Word and I'm trying to past it into text area and then insert into database but it doesn't work. it breaks my query so I want to replace it with this one(') please how do I achieve this.
I tried this but it seem not working
function replace_microsolft_apostrophe($string){
$string = str_replace('’', "'", $string);
return $string;
}
my query looks like this:
function create_note($page_id,$title,$note,$category,$author_id){
global $conn,$notes_table,$pages_table,$notification_table;
$sql = "INSERT INTO $notes_table(page_id,title,content,note_category) VALUES(?,?,?,?)";
$query = $conn->prepare($sql);
$query->bind_param("issi",$page_id,$title,htmlspecialchars($note),$category);
$query->execute();
}
The above query works fine without this character (’)
also I want to know why it this so? because it is the same key i press but when it goes to Microsoft Word the character seem to change. The reason why am trying to make it work is because a user may copy an already typed work from Microsolf word and past on my application where I expect them to write and note and publish it.
Any help would be much good.
WOW! after surfing the net I found Pascal Martin's answer very useful...it also has a reference to a website with the full answer.
How to replace Microsoft-encoded quotes in PHP
I was able to replace right quotation mark with normal quote
//convert single-byte apostrophes -encoded
function convert_smart_quotes($string)
{
$search = array(chr(145),
chr(146),
chr(147),
chr(148),
chr(151));
$replace = array("'",
"'",
'"',
'"',
'-');
return str_replace($search, $replace, $string);
}
Also this answer is much more useful:
Converting Microsoft Word special characters with PHP
This seems to be a charset issue. Have you checked if even your editor's charset is set to ISO-8859-1?
Anyway, a workaround could be convert your string to hex and let MySQL convert it again to string. I haven't tested this code, but it should work.
function create_note($page_id,$title,$note,$category,$author_id){
global $conn,$notes_table,$pages_table,$notification_table;
$sql = "INSERT INTO $notes_table(page_id,title,content,note_category) VALUES(?,?,UNHEX(?),?)";
$query = $conn->prepare($sql);
$query->bind_param("issi",$page_id,$title,bin2hex(htmlspecialchars($note)),$category);
$query->execute();
}

php escaping double quotes

Hi I'm having some difficulty with escaping double quoutes from a string.
Here's my situation:
I get the the result set from the database then I apply utf8_encode to it because there's latin/accented characters and it return the string as it should be exept the double quotes in the begin and end of the string.
If in the DB I have: "Olá João" it returns: Olá João. The double quotes are ignored
$rs = mysql_fetch_array($query)
$text = utf8_encode($rs['l_reference']);
echo $text;
I tried using addslashes but without success.
I think its because "Olá João" is a multibyte string and you must use a different workaround for this. Try this one mb_addslashes

Smart quotes not encoded correctly by Twitter share pop-up in IE7

I am building a Wordpress theme in which I have a twitter share link beside each post
<a class="tw-share-link" href="http://twitter.com/home?status=Currently reading <?php the_article_title() ?>" Target="_blank">Tweet</a>
If the article title includes smart quotes, the quotes are replaced by '?'s in IE8 and below.
I tried converting the smart quotes to regular quotation marks hooking a function that uses
string replace to the action of saving or updating a post. This didn't fix the issue.
?php
function convert_smart_quotes($string) {
//converts smart quotes to normal quotes.
$search = array(chr(145), chr(146), chr(147), chr(148), chr(151));
$replace = array("'", "'", '"', '"', '-');
return str_replace($search, $replace, $string);
}
?>
I need guidance troubleshooting this issue:
+ Do I need to add some sort of character set declaration to my code so that IE8 and below can handle smart-quotes?
+ Or is there a way in php to encode the twitter link so that smart quotes will be replaced by regular quotes? Thanks in advance.
Update:
I found a fix. Removing the wptexturize filter fixes the issue:
http://www.malcolmcoles.co.uk/blog/wordpress-smart-quotes/

replace double quotes with single quotes in a string

I know this sounds simple, and probably is, but I can't seem ot get this working. Just want to replace all occurences of a double quote with a single quote...tired this but it doesn't work:
$con = str_replace("\"", "'", $content);
Or:
$con = str_replace(chr(34), chr(39), $content);
What you do is correct and should work. If it doesn't, then you may only SEE double quotes, but in reality these are other characters. Possible is html " character rendered as ". There are also several chars very similar to double quotes. hey 'happen' especially when pasting text from word or openoffice. You'll include all possibilities in str_replace (it can take arrays of strings as parameters).
I had the same problem with input from a form.
I used &ampquot; for my search string and it worked great.
$con = str_replace("&ampquot;", "'", $content);

Categories