PHP Conference Japan 2024

rtrim

(PHP 4, PHP 5, PHP 7, PHP 8)

rtrim從字串結尾去除空白字元 (或其他字元)

描述

rtrim(string $string, string $characters = " \n\r\t\v\x00"): string

此函式會傳回一個字串,該字串的結尾會去除空白字元 (或其他字元)。

若沒有第二個參數,rtrim() 會去除這些字元

  • " "ASCII SP 字元 0x20,一個普通空格。
  • "\t"ASCII HT 字元 0x09,一個 Tab 字元。
  • "\n"ASCII LF 字元 0x0A,一個換行符號(換行)。
  • "\r"ASCII CR 字元 0x0D,一個歸位符號。
  • "\0"ASCII NUL 字元 0x00,NUL 位元組。
  • "\v"ASCII VT 字元 0x0B,一個垂直 Tab 字元。

參數

string
輸入字串。
characters
選擇性地,也可以使用 characters 參數來指定要去除的字元。只需列出所有需要去除的字元即可。 使用 .. 可以指定遞增的字元範圍。

傳回值

傳回修改後的字串。

範例

範例 #1 rtrim() 的用法範例

<?php

$text
= "\t\tThese are a few words :) ... ";
$binary = "\x09Example string\x0A";
$hello = "Hello World";
var_dump($text, $binary, $hello);

print
"\n";

$trimmed = rtrim($text);
var_dump($trimmed);

$trimmed = rtrim($text, " \t.");
var_dump($trimmed);

$trimmed = rtrim($hello, "Hdle");
var_dump($trimmed);

// trim the ASCII control characters at the end of $binary
// (from 0 to 31 inclusive)
$clean = rtrim($binary, "\x00..\x1F");
var_dump($clean);

?>

上面的範例會輸出

string(32) "        These are a few words :) ...  "
string(16) "    Example string
"
string(11) "Hello World"

string(30) "        These are a few words :) ..."
string(26) "        These are a few words :)"
string(9) "Hello Wor"
string(15) "    Example string"

參見

  • trim() - 從字串的開頭和結尾去除空白字元 (或其他字元)
  • ltrim() - 從字串的開頭去除空白字元 (或其他字元)
新增註解

使用者貢獻的註解 7 則註解

44
pinkgothic at gmail dot com
14 年前
我對 PHP 的陣列函式有種難以自拔的愛,因為它們讓我在各種情況下都能輕鬆處理複雜的字串... 所以,再來一個字串 rtrim() 的變體

<?php

function strrtrim($message, $strip) {
// 依照 strip 字串分解 message
$lines = explode($strip, $message);
$last = '';
// 從結尾彈出空字串
do {
$last = array_pop($lines);
} while (empty(
$last) && (count($lines)));
// 重新組裝剩下的部分
return implode($strip, array_merge($lines, array($last)));
}

?>

令人驚訝的是,我沒想到的是:它在執行時間上與下面 harmor 的 rstrtrim 完全相同。o_o 哇!
30
gbelanger at exosecurity dot com
18 年前
沒錯,Perl 的 chomp() 只會去除換行符號。但是,Perl 還有 chop() 函式,它與 PHP 的 rtrim() 非常相似。

---

這是一種快速遞迴修剪陣列每個元素的方法,在 file() 函式之後很有用

<?php
# 讀取 /etc/passwd 檔案,並修剪每個條目的換行符號
$aFileContent = file("/etc/passwd");
foreach (
$aFileContent as $sKey => $sValue) {
$aFileContent[$sKey] = rtrim($sValue);
}

print_r($aFileContent);
?>
26
todd at magnifisites dot com
21 年前
這顯示了當使用選擇性的 charlist 參數時 rtrim 的運作方式
rtrim 函式會從可選的 charlist 參數中逐個讀取字元,並將其與 str 字串的結尾進行比較。如果字元匹配,它會將其修剪掉,然後再次從 str 字串中「新的」最後一個字元開始,並再次與 charlist 中的第一個字元比較。如果字元不匹配,它會移動到 charlist 參數中的下一個字元,再次進行比較。這個過程會持續進行,直到 charlist 參數被完全處理完畢,且 str 字串不再包含任何匹配的字元。最後會回傳新修剪後的字串。
<?php
// 範例 1:
rtrim('This is a short short sentence', 'short sentence');
// 回傳 'This is a'
// 如果您預期結果是 'This is a short ',
// 那就錯了;它不會匹配整個字串 'short sentence',
// 請記住,它是逐個字元進行比較!
// 範例 2:
rtrim('This is a short short sentence', 'cents');
// 回傳 'This is a short short '
?>
21
Unimagined at UnaimaginedDesigns dot Com
19 年前
我需要一個方法來修剪掉字串結尾的所有空白字元,然後再修剪掉一些特定的字串。所以我寫了這個類別,以便在需要修剪字串時重複使用。

<?php

class cleaner {

function
cleaner ($cuts,$pinfo) {
$ucut = "0";
$lcut = "0";
while (
$cuts[$ucut]) {
$lcut++;
$ucut++;
}
$lcut = $lcut - 1;
$ucut = "0";
$rcut = "0";
$wiy = "start";

while (
$wiy) {

if (
$so) {
$ucut = "0";
$rcut = "0";
unset(
$so);
}

if (!
$cuts[$ucut]) {
$so = "restart";
} else {
$pinfo = rtrim($pinfo);
$bpinfol = strlen($pinfo);
$tcut = $cuts[$ucut];
$pinfo = rtrim($pinfo,"$tcut");
$pinfol = strlen($pinfo);

if (
$bpinfol == $pinfol) {
$rcut++;
if (
$rcut == $lcut) {
unset(
$wiy);
}
$ucut++;
} else {
$so = "restart";
}
}
}

$this->cleaner = $pinfo;
}

}

$pinfo = "Well... I'm really bored...<br /><br>&nbsp; \n\t&nbsp;<br><br /><br>&nbsp; \r\r&nbsp;<br>\r<br /><br>\r&nbsp; &nbsp;\n<br> <br />\t";

$cuts = array('\n','\r','\t',' ',' ','&nbsp;','<br />','<br>','<br/>');

$pinfo = new cleaner($cuts,$pinfo);
$pinfo = $pinfo->cleaner;

print
$pinfo;

?>

這個類別會從 $pinfo 字串的結尾移除您放入 $cust 陣列中的任何字串。它對於清理使用者發佈到您網站上的評論、文章或郵件非常有用,可以確保沒有多餘的空白或空行。
16
pinkgothic at gmail dot com
11 年前
關於字串剝離而不是字元剝離的 rtrim() 實作的常見問題... 最簡單的方法(有個但書)可能是使用 basename() 函數。它有一個作為右側修剪功能的第二個參數,可以使用整個字串。

<?php

echo basename('MooFoo', 'Foo');

?>

...輸出 'Moo'。

由於它也會剝離任何看起來像目錄的東西,所以它與從結尾刪除字串並不完全相同。

<?php

echo basename('Zoo/MooFoo', 'Foo');

?>

...仍然輸出 'Moo'。

但有時它也能完成工作。
5
harmor
16 年前
我確信有更好的方法可以從字串的結尾移除字串。

<?php
/**
* 從字串的結尾移除字串
*
* @param string $str 輸入字串
* @param string $remove 可選的要移除的字串
*
* @return string 修改後的字串
*/
function rstrtrim($str, $remove=null)
{
$str = (string)$str;
$remove = (string)$remove;

if(empty(
$remove))
{
return
rtrim($str);
}

$len = strlen($remove);
$offset = strlen($str)-$len;
while(
$offset > 0 && $offset == strpos($str, $remove, $offset))
{
$str = substr($str, 0, $offset);
$offset = strlen($str)-$len;
}

return
rtrim($str);

}
//End of function rstrtrim($str, $remove=null)

echo rstrtrim('Hello World!!!', '!') .'<br />'; //"Hello World"
echo rstrtrim('Hello World!!!', '!!') .'<br />'; //"Hello World!"
echo rstrtrim('Hello World!!!', '!!!') .'<br />'; //"Hello World"
echo rstrtrim('Hello World!!!', '!!!!').'<br />'; //"Hello World!!!"
?>
-2
HW
21 年前
<?php
$text
= "This string contains some unwanted characters on the end.";
$text1 = rtrim($text, 'a..z');
$text1 = rtrim($text1, '.');
echo
$text1; // only the '.' is trimmed.
$text2 = rtrim($text, 'a..z.');
echo
$text2; // The whole last word is trimmed.
?>
To Top