简体   繁体   中英

PHP character encoding issue

When I grab the title from my Word Press posts in code and pass them around as email, the punctuation gets a bit mangled.

For example "TAMAGOTCHI P'S LOVE & MELODY SET" comes out as "TAMAGOTCHI P’S LOVE & MELODY SET" .

Any ideas how I prevent this?

Let me know if you need to see the specific code I'm currently using. (I'm not really sure if this is a WordPress issue, or a PHP issue.

EDIT What happens is that this title is passed to a form via the query string. Then when the form is submitted, I take the string from the form field and email it.

So I guess I need to decode the html either before I pass it into the form field, or else before I email it.

EDIT 2 Weird, so I looked closer at the code and I'm already doing a urldecode before I pass the value into the form field

jQuery('#product_name').val("<?php echo urldecode(strip_tags($_GET['pname'])); ?>

Is there some default encoding happening when you serialize (for ajax formhandler)

    var dataString = $(this).serialize();

EDIT 3 OK turns out the code is more complex. Title is also passed to some kind of wordpress session before it's hits the form. I'll figure it out where exactly I need to put urldecode. Thanks!

This is one WordPress "feature" I could do without.

Here's one down-n-dirty method to get the fancy quotes (or other entities) replaced:

$title = get_the_title( get_the_ID() );
$title = str_replace( '&#8217', "'", $title );
echo $title;

We could integrate deeper, by hooking into the_title , if you want this same de-entities functionality throughout the site. This code block would belong in your theme's functions.php file.

function reform_title($title, $id) {
    $title = str_replace( '&#8217', "'", $title );
    return $title;
}
add_filter('the_title', 'reform_title', 10, 2);

Im not really sure about wordpress, but the issue itself its that the text its coming out as URLENCODE instead of a UTF-8 or other encode.

You have two options

  1. When you receive the text you never turn it back to normal encoding (Which is weird as usually is de-encoded by php when you access the $_GET or $_POST variables)

  2. You are parsing the message with the urlencode() function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM