2012-11-21 126 views
-2

所以我目前有一系列的字符串。他們可能會訪問空格,所以我想通過將「book」轉換爲「book」來擺脫它。但是,如果我使用修剪,諸如「某些標題」之類的字符串變成「sometitle」。我如何去做這件事?清除字符串周圍的空白而不刪除空格以及

+4

'修剪( 「有些題」)== 「sometitle」' - [手冊! ](http://www.php.net/trim) –

+3

@Jayson如果你要鏈接到一個函數,請使用PHP手冊(http://php.net/trim)不是那個可怕的網站(這是爲什麼: http://w3fools.com/) – 2012-11-21 02:48:41

回答

2

修剪是你正在尋找的功能,文檔here。與你所說的相反:

trim — Strip whitespace (or other characters) from the beginning and end of a string 

中間不會受到影響!

0
$str[] = " book "; 
$str[] = " book"; 
$str[] = "book "; 
$str[] = "b o o k"; 
foreach($str as $s){ 
    echo trim($s).'<br>'; 
} 

trim()切割前後白色空間。從來不在中間。

0

使用:

trim(" Some title"); 

不會用言語之間移除空間 - 僅在一個字符串,ERGO結果的起點和終點:

"Some title" 

也有ltrim()rtrim()功能,它們分別從字符串的左邊(開始)和右邊(末尾)刪除字符。

你可以閱讀更多關於他們在這裏: