3

好吧,我已经尝试过这些,似乎它们都不起作用,我的示例字符串是

 $text_description="       Hello world! lorel ipsum";

 $text_description= str_replace(" "," ",$text_description);
 $text_description = preg_replace("/&#?[a-z0-9]+;/i"," ",$text_description);
 $text_description=html_entity_decode($text_description);
4

3 回答 3

5
$text_description="       Hello world! lorel ipsum";
$text_description = str_replace(' ', ' ', $text_description);
echo $text_description;

输出:

你好世界!洛雷尔伊普苏姆

于 2012-07-31T05:14:29.360 回答
4

回答有点晚,但希望可以帮助其他人。从 html 中提取内容最重要的是在 php.ini 中使用utf8_decode()。然后所有其他字符串操作变得轻而易举。甚至外来字符也可以通过直接从浏览器复制粘贴字符到php代码中来替换。以下函数替换 为空字符。然后使用preg_replace(). 最后使用删除前导和尾随空格trim()

function clean($str)
{       
    $str = utf8_decode($str);
    $str = str_replace(" ", "", $str);
    $str = preg_replace('/\s+/', ' ',$str);
    $str = trim($str);
    return $str;
}

$html = "       Hello world! lorel ipsum";
$output = clean($html);
echo $output;

你好世界!洛雷尔伊普苏姆

于 2016-06-29T18:43:37.510 回答
2

您可以只使用html_entity_decode(),它允许您将所有 html 实体替换为其适用的字符,例如:

 $HtmlText="it's Working \"Correctly\"";
 $MyText=html_entity_decode($HtmlText);
 echo $MyText; // "it's Working "Correctly"

我希望这有帮助!;)

于 2013-02-08T16:28:00.733 回答