繁体   English   中英

替换为空白或空字符串PHP

[英]Replace   with a blank or empty string PHP

好吧,我已经尝试了这些,似乎它们都没有工作,我的示例字符串是

 $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);
$text_description="       Hello world! lorel ipsum";
$text_description = str_replace(' ', ' ', $text_description);
echo $text_description;

输出:

你好,世界! lorel ipsum

回答有点迟,但希望可以帮助别人。 从html中提取内容时最重要的是在php中使用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;

你好,世界! lorel ipsum

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

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

我希望这是有帮助的! ;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM