如何使用 PHP 删除上下文中不可读的字符


how to remove unreadable characters in a context using PHP?

嗨,我正在向zend_lucene_search提供上下文,它可以搜索单词到特殊字符,之后就无法搜索了。

例如:

    very well to the other job boards � one of the main things that has impressed is the variety of the applications, especially with regards to the background of the candidates" manoj � Head 

如果我搜索"板",我可以得到它,但如果我在不可读字符之后搜索一个或任何字符串,我无法搜索它。

如何删除这些,我想获得纯文本。

我在将 .docx/pdf 文件转换为文本时得到了这些字符。

让我知道如何只向zend_search_lucene提供文本。

请帮忙。

您可以使用以下函数调用preg_replace从字符串中删除所有非 ASCII(所谓的特殊)字符:

$replaced = preg_replace('/[^'x00-'x7F]+/', '', $str);
// produces this converted text:
//    "very well to the other job boards  one of the main things that has impressed
// is the variety of the applications, especially with regards to the background of the
// candidates" manoj  Head"

您可能需要转换正在处理的字符串的字符集,以匹配当前 HTML 文档的字符集。

例如,如果您的 HTML 文档使用 UTF-8,那么您可以通过 utf8_encode() 运行字符串。否则,如果您不确定要使用哪个字符集,请尝试使用 mb_convert_encoding() 并使用一些更常见的字符集。