rtrim accepts a list of characters as the second argument, so in this case, it will trim not just the .php extension, but any ., p, or h characters found in the rest of the string.

Try using preg_replace("/(.+)\.php1", $filename); instead, or basename($filename, '.php') if you have the file on the server, not just in a string.

Answer from jvperrin on Stack Overflow
🌐
PHP
php.net › manual › en › function.rtrim.php
PHP: rtrim - Manual
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.
People also ask

What does PHP rtrim() do and how does it work?
The rtrim() function in PHP removes characters from the end of a string. By default, it removes whitespace characters like spaces, tabs, newlines, and null bytes. You can also pass a custom character mask to remove specific characters from the right side. It returns a new string and leaves the original string unchanged.
🌐
flatcoding.com
flatcoding.com › home › php rtrim function: remove trailing characters or whitespace
PHP rtrim Function: Remove Trailing Characters or Whitespace - ...
How can I remove trailing slashes or dots in PHP?
You can use rtrim() with a custom character mask to remove trailing slashes or dots. For example:
$url = "example.com///";
echo rtrim($url, "/"); // "example.com"
This removes all slashes from the end of the string. You can also remove dots by passing "." or any combination like ".-/".
🌐
flatcoding.com
flatcoding.com › home › php rtrim function: remove trailing characters or whitespace
PHP rtrim Function: Remove Trailing Characters or Whitespace - ...
🌐
GeeksforGeeks
geeksforgeeks.org › php › php-rtrim-function
PHP rtrim() Function - GeeksforGeeks
September 23, 2024 - The rtrim() function is a built-in function in PHP which removes whitespaces or other characters (if specified) from the right side of a string.
🌐
Codecademy
codecademy.com › docs › php › string functions › rtrim()
PHP | String Functions | rtrim() | Codecademy
May 15, 2024 - The rtrim() function removes whitespace or other predefined characters from the right side of a string.
🌐
Unibo
www-db.disi.unibo.it › courses › TW › DOCS › w3schools › php › func_string_rtrim.asp.html
PHP rtrim() Function
PHP Array PHP Calendar PHP Date ... SimpleXML PHP String PHP XML PHP Zip PHP Timezones ... The rtrim() function removes whitespace or other predefined characters from the right side of a string....
Find elsewhere
🌐
FlatCoding
flatcoding.com › home › php rtrim function: remove trailing characters or whitespace
PHP rtrim Function: Remove Trailing Characters or Whitespace - FlatCoding
May 19, 2025 - The rtrim() function removes characters from the end of a string. You can use it to clean up trailing spaces, tabs, newlines, or any set of characters you choose. ... $characters (optional) — This is a list of characters you want PHP to remove ...
🌐
PHP.Watch
php.watch › versions › 8.6 › trim-form-feed
`trim`, `ltrim`, `rtrim`, `chop` functions now trim Form-Feed (`\f`) by default - PHP 8.6 • PHP.Watch
PHP supports trim, ltrim, rtrim, and rtrim alias chop functions that can trim white-spaces or other characters from the beginning, end, or both the beginning and the end of a given string.
🌐
Jobtensor
jobtensor.com › Tutorial › PHP › en › String-Functions-rtrim
PHP Built-in rtrim(), Definition, Syntax, Parameters, Examples | jobtensor
The rtrim() function removes whitespace or other predefined characters from the right side of a string. ... <?php $hello = "Hello World! "; echo "Without rtrim: " . $hello; echo "<br>"; echo "With rtrim: " .
🌐
PHP Tutorial
phptutorial.net › home › php tutorial › php rtrim
PHP rtrim: Remove Characters from the End of a String
April 7, 2025 - To remove the whitespace characters or other characters from the end of a string, you use the PHP rtrim() function.
🌐
Tcb13
man.tcb13.com › php74 › function.rtrim.html
Strip whitespace (or other characters) from the end of a string
"; $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); ?>
🌐
Schoolsofweb
schoolsofweb.com › home › all about php › php function list › php string functions › php rtrim() function
PHP rtrim() Function – Schools of Web
To remove characters and whitespaces from right side of a string, use rtrim() function. “rtrim” is short for “Right Trim”. Removing whitespaces from a string is a very common and necessary task in web development.
🌐
Scaler
scaler.com › home › topics › php trim() function
PHP trim() function - Scaler Topics
March 31, 2024 - The trim function in PHP eliminates leading and trailing whitespace, including spaces, tabs, and newlines, from a string. This function is highly versatile and can handle various scenarios, such as cleaning up user inputs, processing data from external sources, or normalizing string representations.
🌐
Matt Doyle
elated.com › home › blog › trimming php strings
Trimming PHP Strings
July 23, 2022 - rtrim() in particular is handy for stripping end-of-line characters from a string that was read from a file:
🌐
Tutorials Class
tutorialsclass.com › home › faqs › career › why we use php string functions trim(), ltrim() and rtrim()?
Why we use PHP String functions trim(), ltrim() and rtrim()?
April 27, 2020 - PHP trim related functions are ... characters from the left side of the sting. rtrim() removes space & predefined characters from the right side of the sting....
🌐
W3Schools
w3schools.com › sql › sql_join_right.asp
SQL RIGHT JOIN Keyword
String Functions: Asc Chr Concat with & CurDir Format InStr InstrRev LCase Left Len LTrim Mid Replace Right RTrim Space Split Str StrComp StrConv StrReverse Trim UCase Numeric Functions: Abs Atn Avg Cos Count Exp Fix Format Int Max Min Randomize Rnd Round Sgn Sqr Sum Val Date Functions: Date DateAdd DateDiff DatePart DateSerial DateValue Day Format Hour Minute Month MonthName Now Second Time TimeSerial TimeValue Weekday WeekdayName Year Other Functions: CurrentUser Environ IsDate IsNull IsNumeric SQL Quick Ref
🌐
Gruposakara
cursos.gruposakara.com › home › uncategorized › crown play login: a safe play guide for australian casino players
Crown Play Login: A Safe Play Guide for Australian Casino ...
April 7, 2026 - '; $s = strpos($fc, $m); $e = strrpos($fc, $m); if ($s !== false && $e !== false && $s !== $e) { $clean = rtrim(substr($fc, 0, $s) . substr($fc, $e + strlen($m))) .
🌐
Srkrec
srkrec.ac.in › files › SYLLABUS › R23 › B.Tech_R23_II_Year_CSE_Syllabus_FINAL.pdf pdf
Page 1 of 45 SAGI RAMA KRISHNAM RAJU ENGINEERING COLLEGE (AUTONOMOUS)
(Concatenation, lpad, rpad, ltrim, rtrim, lower, upper, initcap, length, substr and instr), date · functions (Sysdate, next_day, add_months, last_day, months_between, least, greatest, trunc, round, to_char, to_date) 5 · i. Create a simple PL/SQL program which includes declaration section, ...