簡體   English   中英

如何從PHP中的字符串的開頭和結尾刪除尾隨空格和“”?

[英]How to remove trailing white spaces and “ ” from the start and end of a string in PHP?

如果

$text = '           MEANINGFUL THINGS GO HERE         ';

我怎樣才能得到

$cleanText = 'MEANINGFUL THINGS GO HERE';

我知道以下將刪除所有的空格

$text=trim($text);

但是如何將實際逃逸的空間納入裝飾呢?

Meaningful Things可以包含[shortcodes]代碼[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));

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM