Remove Trailing Slash From String PHP
Sure it is, simply check if the last character is a slash and then nuke that one.
if(substr($string, -1) == '/') {
$string = substr($string, 0, -1);
}
Another (probably better) option would be using rtrim()
- this one removes all trailing slashes:
$string = rtrim($string, '/');
Find trailing slash and delete all after it then delete slash
You can get position of the last slash by strrpos() method and then you just remove the string after that position by substr_replace()
like,
$url = "https://db.ygoprodeck.com/card/Qliphort%20Shell/?_ga=2.211230173.973474856.1550500277-1234167223.1550500277";
echo $new = substr_replace($url,'',strrpos($url, '/'));
Thank you.
Removing a forward-slash from the tail-end of an URL
$site = preg_replace('{/$}', '', $site);
This uses a relatively simple regular expression. The $
means only match slashes at the end of the string, so it won't remove the first slash in stackoverflow.com/questions/
. The curly braces {}
are just delimiters; PHP requires matching characters and the front and back of regular expressions, for some silly reason.
Remove first occurance of trailing slash in php
You can use the ltrim
function as:
$result = ltrim( ltrim($dir['subdir']), '/');
The inner ltrim removes leading whitespace and the outer ltrim removes the /
as you want it.
Related Topics
How to Pass Parameters into a PHP Script Through a Webpage
Hunting Cheaters in a Voting Competition
How to Get the Last 7 Characters of a PHP String
How to Get Current PHP Page Name
Update Command-Line Output, I.E. for Progress
PHP Tree Structure for Categories and Sub Categories Without Looping a Query
$Php_Autoconf Errors on MAC Os X 10.7.3 When Trying to Install Pecl Extensions
How to Configuring a Xampp Web Server for Different Root Directory
How to Install Composer Globally on Windows
How to Properly Handle Session and Access Token with Facebook PHP Sdk 3.0
Difference Between Break and Continue in PHP
Accessing Files Relative to Bundle in Symfony2
How to Implement Ocr on a Website Using PHP
Php's Array_Map Including Keys
Composer Install Error - Requires Ext_Curl When It's Actually Enabled
Laravel Eloquent - Attach VS Sync