Hướng dẫn php strip trailing character

I have some URLs, like www.amazon.com/, www.digg.com or www.microsoft.com/ and I want to remove the trailing slash, if it exists, so not just the last character. Is there a trim or rtrim for this?

Nội dung chính

  • Not the answer you're looking for? Browse other questions tagged php or ask your own question.
  • PHP String: Exercise-21 with Solution
  • PHP: Tips of the Day

trejder

16.7k26 gold badges118 silver badges210 bronze badges

asked Mar 6, 2010 at 13:59

0

Simple and works across both Windows and Unix:

$url = rtrim($url, '/\\')

answered Apr 13, 2015 at 11:02

Dario FumagalliDario Fumagalli

9141 gold badge15 silver badges21 bronze badges

2

I came here looking for a way to remove trailing slash and redirect the browser, I have come up with an answer that I would like to share for anyone coming after me:

//remove trailing slash from uri
if( ($_SERVER['REQUEST_URI'] != "/") and preg_match('{/$}',$_SERVER['REQUEST_URI']) ) {
    header ('Location: '.preg_replace('{/$}', '', $_SERVER['REQUEST_URI']));
    exit();
}

The ($_SERVER['REQUEST_URI'] != "/") will avoid host URI e.g www.amazon.com/ because web browsers always send a trailing slash after a domain name, and preg_match('{/$}',$_SERVER['REQUEST_URI']) will match all other URI with trailing slash as last character. Then preg_replace('{/$}', '', $_SERVER['REQUEST_URI']) will remove the slash and hand over to header() to redirect. The exit() function is important to stop any further code execution.

answered Dec 6, 2015 at 5:24

Hướng dẫn php strip trailing character

1

$urls="www.amazon.com/ www.digg.com/ www.microsoft.com/";
echo preg_replace("/\b\//","",$urls);

answered Mar 6, 2010 at 14:23

ghostdog74ghostdog74

313k55 gold badges252 silver badges339 bronze badges

4

Not the answer you're looking for? Browse other questions tagged php or ask your own question.

Last update on August 19 2022 21:50:38 (UTC/GMT +8 hours)

PHP String: Exercise-21 with Solution

Write a PHP script to remove trailing slash from a string.

Original String : 'The quick brown fox jumps over the lazy dog///'

Pictorial Presentation:

Sample Solution:

PHP Code:



Sample Output:

The quick brown fox jumps over the lazy dog

Flowchart :

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to remove part of a string.
Next: Write a PHP script to get the characters after the last '/' in an url.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.

PHP: Tips of the Day

PHP: How to get the file extension in PHP?

No need to use string functions. You can use something that's actually designed for what you want: pathinfo():

$path = $_FILES['image']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);

Ref : https://bit.ly/31VUJzu


  • Exercises: Weekly Top 16 Most Popular Topics
  • SQL Exercises, Practice, Solution - JOINS
  • SQL Exercises, Practice, Solution - SUBQUERIES
  • JavaScript basic - Exercises, Practice, Solution
  • Java Array: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : Conditional Statement
  • HR Database - SORT FILTER: Exercises, Practice, Solution
  • C Programming Exercises, Practice, Solution : String
  • Python Data Types: Dictionary - Exercises, Practice, Solution
  • Python Programming Puzzles - Exercises, Practice, Solution
  • C++ Array: Exercises, Practice, Solution
  • JavaScript conditional statements and loops - Exercises, Practice, Solution
  • C# Sharp Basic Algorithm: Exercises, Practice, Solution
  • Python Lambda - Exercises, Practice, Solution
  • Python Pandas DataFrame: Exercises, Practice, Solution
  • Conversion Tools
  • JavaScript: HTML Form Validation