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

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

Related

substr and strip_tags character issue and solution

I'm calling same text for php but result is chracter problem. e.g;
"<div class="open"><?php echo ex_substr(180); ?></div>"
and run the function
function ex_substr($char) {
$title= get_the_excerpt($post->ID);
$title= strip_tags(substr($title,0,$char));
echo $title."...";
}
At the end of the text content,there are character problems for example: �
I'm very new about php.
as I said in the comments when you see weird stuff like � you probably have some weird non UTF-8 characters in there.
While I am by no means an expert at encodings ( typical I just remove that junk ) This is what I use to deal with them
preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
Here is a PHP sandbox you can try it in.
http://sandbox.onlinephpfunctions.com/code/695ad38ae46ef5a142102dd6150fd84279b1a058
$string ='this is a string �';
echo preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $string);
Output
'this is a string '
You can smack it with trim($string) if that extra space at the end bugs you.
Another trick is to replace it with a space . and then remove any multiple spaces. That way if it's between words it doesn't just jam them together.
$string ='this is a�string';
$string = preg_replace('/[\x00-\x1F\x80-\xFF]/', ' ', $string);
echo trim( preg_replace('/\s{2, }/', ' ', $string)); //replace 2 or more spaces with a single space.
Even when the DB charset set to UTF-8 I occasionally get weird stuff in my DB. We have a lot of content that is scrapped off the web so who knows what that crap is.
This is sort of a "rudimentary" way to fix it, but if you're not worried about loosing those characters than it should be fine. I should study up more on encoding issues, now that I don't have PHP6 to fix all that for me.
( PS from what i understand PHP6 was supposed to have more support for multi=byte strings, but because of the short comings of C it all fell apart, which is why we went from PHP5 to PHP7 )
Hope that helps.
$title= strip_tags(substr($title,0,$char));
instead of
$title= mb_substr($title,0,$char);
The problem is completely resolved when I use it.

PHP highlight query and escape html special characters

I'm trying to program a search function that hightlights the search query in the result. At the moment I'm using this Code $hightlight = preg_replace('/'.strtolower($query).'/', '<span class=hightlight>'.strtolower($query).'</span>', strtolower($text)); for highlighting, which works fine. The text I'm searching in is a string from a database. The problem now is if the text contains some html special characters, and is for example <test> and the user searches for <te I get the following result: <span class="hightlight"><te< span="">st></te<></span> which is interpretated as st>. This makes sense, but I don't want this. I want <test> as result with <te highlighted. So I need to escape the special characters. I know that there is the function htmlspecialchars, but how can I use it in this case? Or another function? I can't escape them before searching, because than I'm also searching in the HTML-Codes. I also can't escape them after searching, because than are the <span> Tags in the text and they will also be converted to HTML-Codes. I hope you understand my problem. Has anyone a solution for that?
Using a combination of htmlspecialchars() and a regex negative lookahead, I think we're able to solve this.
<php
$text = "this is just my really basic <test> of words";
$query = "<te";
$text = htmlspecialchars($text);
$query = htmlspecialchars($query);
$highlight = preg_replace('/'.strtolower($query).'(?![^\&]*\;)/', '<span class=highlight>'.strtolower($query).'</span>', strtolower($text));
echo $highlight;
?>
(small note, I took the liberty of changing hightlight to highlight)
DEMO
The part of this that solves the issue mentioned in your comment is the negative lookahead: (?![^\&]*\;)
That basically means anything not between & and ;.
Now, this could obviously run into issues in some edge cases where & and ; are both part of the actual text. If you're not doing any sort of text and query limitation/sanitation, I'm not sure that there's anything that will work for all possible cases.

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

Change spaces to -

I am inserting Alias field for my db called $alias
how do I code (I am using php for mysql insert)
to remove all spaces and replace space with "-" (trying to change it to "weburl format" ie removing spaces)
Thanks
Here's the method I use to santize strings for SEF urls:
$slug = trim(strtolower($value));
$slug = preg_replace('/[^a-z0-9 _-]/', '', $slug);
return preg_replace('/\s+/', '-', $slug);
Feel free to add additional allowed characters to the first regex.
Please note that this is NOT Unicode or even full ISO-8891 safe, well, it is, but it'll drop anything that isn't a-z. That is, you may need to normalize the string beforehand (i.e., replace accented characters with their closes ASCII equivalent.) There's a number of SO questions and answers dealing with this that I've seen before, but I can't find them at the moment. I'll edit them in here if I stumble upon any.
For just removing spaces, you want the str_replace method. However, when working with URLs, you might want to consider the urlencode and rawurlencode methods as well.

Categories