2012-05-10 45 views
1

我有用户输入并使用htmlentities()来转换所有实体。 但是,似乎有一些错误。当我在php编码问题htmlentities

ääää öööö üüüü ääää 

型我得到

ääää öööö üüüü ääää 

它看起来像这样

ääää à ¶Ã ¶Ã ¶Ã ¶Ã¼Ã¼Ã¼Ã¼ ääää

我在做什么错?该代码是真的只有这个:

$post=htmlentities($post); 

编辑1

下面是一些我使用的格式的目的(也有一些有用的功能,但它们)更多的代码:

//Secure with htmlentities (mysql_real_escape_string() comes later) 
    $post=htmlentities($post); 

    //Strip obsolete white spaces 
    $post = preg_replace("/ +/", " ", $post); 

    //Detect links 
    $pattern_url='~(?>[a-z+]{2,}://|www\.)(?:[a-z0-9]+(?:\.[a-z0-9]+)[email protected])?(?:(?:[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])(?:\.[a-z](?:[a-z0-9]|(?<!-)-)*[a-z0-9])+|(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?:/[^\\/:?*"<>|\n]*[a-z0-9])*/?(?:\?[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?(?:&[a-z0-9_.%]+(?:=[a-z0-9_.%:/+-]*)?)*)?(?:#[a-z0-9_%.]+)?~i'; 
    preg_match_all($pattern_url, $post, $matches); 
    for ($i=0; $i < count($matches[0]); $i++) 
    { 
     if(substr($matches[0][$i],0,4)=='www.') 
     $post = str_replace($matches[0][$i],'http://'.$matches[0][$i],$post); 
    } 
    $post = preg_replace($pattern_url,'<a target="_blank" href="\\0">\\0</a>',$post); 

    //Keep line breaks (more than one will be stripped above) 
    $post=nl2br($post); 

    //Remove more than one linebreak 
    $post=preg_replace("/(<br\s*\/?>\s*)+/", "<br/>", $post); 

    //Secure with mysql_real_escape_string() 
    $post=mysql_real_escape_string($post); 
+1

当你说“真的只有这个”时,你能分享它的其余部分吗?我没有看到你的PHP有什么问题,所以这个问题可能在别的地方。 –

+0

@stevether请参阅问题编辑。 – weltschmerz

回答

7

你必须手动htmlentities()指定编码(UTF-8):

echo htmlentities("ääää öööö üüüü ääää", null, "UTF-8"); 

输出:

ääää öööö üüüü ääää 
+0

谢谢!那是我需要的。参数null做什么?你也许知道我为什么需要这个?我通常只使用htmlemtities('字符串'),没有任何额外的参数,它通常工作正常。 – weltschmerz

+2

它只是说使用第二个参数的默认值。参数2和参数3是可选的,但是如果您想指定第三参数,则必须指定第二参数。将相当于'htmlentities(“string”,ENT_COMPAT | ENT_HTML401,“UTF-8”)' – SupremeDud

+0

谢谢!奇迹般有效。 – weltschmerz

2

htmlentities的第3个参数与使用该帖子的字符集匹配很重要。我supouse,你是不是submiting UTF8,因为它是在表中ヶ辆

在PHP

$post = htmlentities ($post, ENT_COMPAT, 'ISO-8859-1') // or whatever 

默认

<form action="your.php" accept-charset="ISO-8859-1"> 

反正actualy我建议你使用UTF8

+0

我这样做,谢谢:) – weltschmerz