PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

setlocale> <quotemeta
Last updated: Sun, 25 Nov 2007

view this page in

rtrim

(PHP 4, PHP 5)

rtrim — 문자열 끝 부분의 공백을 제거합니다.

설명

string rtrim ( string $str [, string $charlist ] )

Note: 두번째 인자는 PHP 4.1.0에서 추가되었습니다.

str 의 끝 부분에서 공백을 제거한 문자열을 반환합니다. 두번째 인자가 없으면, rtrim()은 다음 문자들을 제거합니다:

  • " " (ASCII 32 (0x20)), 보통의 공백.
  • "\t" (ASCII 9 (0x09)), 탭.
  • "\n" (ASCII 10 (0x0A)), 뉴라인. (줄바꿈)
  • "\r" (ASCII 13 (0x0D)), 캐리지 리턴.
  • "\0" (ASCII 0 (0x00)), NUL 바이트.
  • "\x0B" (ASCII 11 (0x0B)), 수직 탭.

charlist 인자를 통해서 제거하기를 원하는 문자를 지정할 수 있습니다. 간단히 제거하기를 원하는 모든 문자를 기록하면 됩니다. ..로 문자의 범위를 지정할 수 있습니다.

Example#1 rtrim() 사용 예제

<?php

$text 
"\t\tThese are a few words :) ...  ";
$trimmed rtrim($text);
// $trimmed = "\t\tThese are a few words :) ..."
$trimmed rtrim($text" \t.");
// $trimmed = "\t\tThese are a few words :)"
$clean rtrim($binary"\0x00..\0x1F");
// $binary의 마지막 부분에서 아스키 제어 문자를 제거합니다.
// (0에서 31까지)

?>

참고: trim(), ltrim().



setlocale> <quotemeta
Last updated: Sun, 25 Nov 2007
 
add a note add a note User Contributed Notes
rtrim
harmor
04-Apr-2008 04:05
I'm sure there's a better way to strip strings from the end of strings.

/**
 * Strip a string from the end of a string
 *
 * @param string $str      the input string
 * @param string $remove   OPTIONAL string to remove
 * 
 * @return string the modified 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!!!"
YAS
08-May-2006 06:01
To remove an unwanted character - example "." - if exist or not.

The example above doesn't include the case where there is no "."
If there is not "." at the example above the last word will be deleted.

Have fun with this code.

<?php
$text
= "This string contains. some unwanted characters on the end .";
$text = trim($text);
$last = $text{strlen($text)-1};
if (!
strcmp($last,"."))
{
 
$text = rtrim($text, 'a..z');
 
$text = rtrim($text, '.');
}
?>
gbelanger at exosecurity dot com
17-Feb-2006 02:31
True, the Perl chomp() will only trim newline characters. There is, however, the Perl chop() function which is pretty much identical to the PHP rtrim()

---

Here's a quick way to recursively trim every element of an array, useful after the file() function :

# Reads /etc/passwd file an trims newlines on each entry
$aFileContent = file("/etc/passwd");
foreach ($aFileContent as $sKey => $sValue) {
    $aFileContent[$sKey] = rtrim($sValue);
}

print_r($aFileContent);
Unimagined at UnaimaginedDesigns dot Com
16-Jan-2005 12:49
I needed a way to trim all white space and then a few chosen strings from the end of a string.  So I wrote this class to reuse when stuff needs to be trimmed. 

<?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;

?>

That class will take any string that you put in the $cust array and remove it from the end of the $pinfo string.  It's useful for cleaning up comments, articles, or mail that users post to your site, making it so there's no extra blank space or blank lines.
todd at magnifisites dot com
19-Aug-2003 06:19
This shows how rtrim works when using the optional charlist parameter:
rtrim reads a character, one at a time, from the optional charlist parameter and compares it to the end of the str string. If the characters match, it trims it off and starts over again, looking at the "new" last character in the str string and compares it to the first character in the charlist again. If the characters do not match, it moves to the next character in the charlist parameter comparing once again. It continues until the charlist parameter has been completely processed, one at a time, and the str string no longer contains any matches. The newly "rtrimmed" string is returned.
<?php
 
// Example 1:
 
rtrim('This is a short short sentence', 'short sentence');
 
// returns 'This is a'
  // If you were expecting the result to be 'This is a short ',
  // then you're wrong; the exact string, 'short sentence',
  // isn't matched.  Remember, character-by-character comparison!
  // Example 2:
 
rtrim('This is a short short sentence', 'cents');
 
// returns 'This is a short short '
?>
HW
05-Jun-2003 06:32
$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.
icon-phpnet at phy dot duke dot edu
01-Apr-2002 10:00
Not entirely. Perl's "chomp" will only remove the newline character, while rtrim without the second parameter will remove ALL whitespace. E.g. chomp("blah \n") will return "blah ", while rtrim("blah \n") will return "blah".

setlocale> <quotemeta
Last updated: Sun, 25 Nov 2007
 
 
show source | credits | stats | sitemap | contact | advertising | mirror sites