我有这个文...
“我不是要让人信服,”大卫笑着承认道。
...我想删除那些有趣的字符,我试过str_replace()
了,但它不起作用。
有任何想法吗?
您可能已经以与源编码不同的编码处理文本。
因此,如果文本是 UTF-8,则您当前不会将其作为 UTF-8 处理。最简单的方法是发送标头,例如...
header('Content-Type: text/html; charset=UTF-8');
您也可以添加meta
元素,但要确保它是元素的第一个子head
元素。
您需要在源头上修复它,而不是稍后尝试修补它(这将永远无法正常工作)。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
</head>
不同的源通常具有不同的编码,因此您需要指定您呈现视图的编码。Utf-8 是最流行的,因为它涵盖了所有 ASCII 和许多其他语言。
php 的 utf8_(de)encode 将 iso-8859-1 转换为 utf-8,而相反的常规字符串操作函数不是多字节(utf-8 可以是)字符感知的。您要么使用特定于 mb_strings 的函数,要么使用某些参数启用编码。
//如果我记错了评论
好吧,您正在使用您可能应该使用的不同字符编码(您应该使用 utf-8 编码),所以我会更改它而不是尝试通过快速修复在现场修复它(您会遇到总体上这样的问题更少)。
如果你真的想用 PHP 修复它,你可以使用 ctype_alpha() 函数;你应该能够做这样的事情:
$theString = "your text here"; // your input string
$newString = ""; // your new string
$i = 0;
while($theString[$i]) // while there are still characters in the string
{
if(ctype_alpha($theString[$i]) // if it's a character in your current set
{
$newString .= $theString[$i]; // add it to the new string, increment pointer, and go to next loop iteration
$i++;
continue;
} // if the specific character at the $i index is an alphabetical character, add it to the new string
else
{
$i++;
} // if it's a bad character, just move the pointer up by one for the next iteration
}
然后根据需要使用 $newString 。真的,只是改变你的字符编码而不是这样做。您希望整个项目的编码都相同。