如果
$text = ' MEANINGFUL THINGS GO HERE ';
我怎样才能得到
$cleanText = 'MEANINGFUL THINGS GO HERE';
我知道下面将删除所有的空格
$text=trim($text);
但如何能结合实际躲过空间到装饰呢?
Meaningful Things
可以包含[shortcodes]
,html标签,也转义字符。 我需要这些被保存下来。
任何帮助,将不胜感激。 谢谢!
$text = ' MEANINGFUL THINGS GO HERE ';
$text = preg_replace( "#(^( |\s)+|( |\s)+$)#", "", $text );
var_dump( $text );
//string(25) "MEANINGFUL THINGS GO HERE"
附加测试
$text = ' S S ';
-->
string(24) "S S"
$text = ' ';
-->
string(0) ""
$text = ' &nbst; &nbst; ';
-->
string(18) "&nbst; &nbst;"
同时运行html_entity_decode这个,再修剪一下:
$text=trim(html_entity_decode($text));
文章来源: How to remove trailing white spaces and “ ” from the start and end of a string in PHP?