duanbu9345
duanbu9345
2012-03-13 19:42

在php中访问textarea内容时的特殊字符

  • string
  • textarea
  • php
  • forms
已采纳

I am getting special characters from the textarea input on my form

So if I enter the below text

Philbrick, whose “In the Heart of the Sea: The Tragedy of the Whaleship Essex” recounted the real-life inspiration for Melville’s shipwreck, wears his erudition lightly.

I get the following when I access it in php.

Philbrick, whose “In the Heart of the Sea: The Tragedy of the Whaleship Essex†recounted the real-life inspiration for Melville’s shipwreck, wears his erudition lightly.

I tried the below before accessing the text , but replace is not working. Are these special characters not matching when I am copy pasting those in the code or its something else which is missing?

$string = htmlspecialchars($_POST["t"]) ;
$string = html_entity_decode($string, ENT_QUOTES, "UTF-8");
$string = str_replace('“', "\"", $string);
$string = str_replace('’', "'", $string);
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dtsi9484 dtsi9484 9年前

    There may not be anything wrong with what you're seeing. You may not need to mangle the $_POST with htmlentities corrections or otherwise. It could be that you're looking at a barebones browser output.

    In case you're debugging this by spitting it out with something like print_r( $_POST ); die;, make sure that somewhere above it in your page you have a meta line setting your character encoding to UTF-8:

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    That will ensure that the smart characters (presumably pasted from somewhere) render properly on screen.

    Completely unrelated: Excellent book, BTW. Love the beginning scene with gaunt sailors hoarding their piles of bones.

    点赞 评论 复制链接分享
  • dongzuoyue6556 dongzuoyue6556 9年前

    You actually have not an ordinary single and double quotes, but some fancy unicode version: &#x201c; &#x201d &#x2019

    Try utf8_decode. BTW what encoding do you use, Windows-1250?

    点赞 评论 复制链接分享

为你推荐