How to replace apostrophe () with single quote (')

There seem to be two types of apostrophes. I want to know the difference between this symbol () and this ( ' )

the first one was copied from Microsoft Word and I try to loop it through the text area and then paste it into the database, but it doesn't work. it interrupts my request, so I want to replace it with this ("), please how can I achieve this.

I tried this but doesn't seem to work

function replace_microsolft_apostrophe($string){
        $string = str_replace('’', "'", $string);
        return $string;
    }

      

my request 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 without this character ()

Also I want to know why this is so? because it is the same key I press, but when it goes into Microsoft Word the character seems to change. The reason I am trying to get it to work is because the user can copy the already printed work from the word Microsolf and in the past in my application where I expect them to write and notice and publish it.

Any help would be very good.

+4


source to share


2 answers


BLIMEY! after surfing the net I found Pascal Martin's answer very useful ... it also has a link to a site with a complete answer. How to replace Microsoft quotes in PHP

I was able to replace the right quote with a regular 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 helpful: converting Microsoft Word special characters with PHP

+4


source


This seems to be an encoding issue. Have you checked if even your editors encoder is installed on ISO-8859-1

?

Anyway, a workaround could convert your string to hex and let MySQL convert it back to a string again. I have not 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();
}

      

+1


source







All Articles