13

The code below removes "www.", etc. from the beginning of websites that are entered into a database. It works great.

Is there a way I could use similar code to remove a forward-slash from the tail-end of a website that is entered into the same database?

$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$site = str_replace($remove_array, "", $_POST['site']);
0

9 Answers 9

48

You can pass a string of characters that you want trimmed off of a string to the trim family of functions. Also, you could use rtrim to trim just the end of the string:

$site = rtrim($site, "/");
Sign up to request clarification or add additional context in comments.

3 Comments

regexps are cannons, trailing slashes are sparrows. altough i'd remove more than just trailing slashes, e.g. spaces, tabs and linebreaks: $site = rtrim($site, "/ \t\n\r");
also, rtrim removes all trailing slashes $site = 'foo.bar/////", while the regexp only removes one trailing slash.
This should be the accepted answer. Don't use regex unless you really have to.
25
$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.

6 Comments

I thought the matching chars had to be the same, i.e. { and { but not { and }
Also, the chars are useful when you want to supply flags. e.g. /[a-z]*/i means match everything in a case insensitive way.
(), {}, [], and <> are exceptions. IMHO, the flags ought to be a separate parameter. 95% of the time I don't have any flags and the delimiters are just noise.
Ah I see, thanks for letting me know. I generally go with / to be traditional but they can be a pain when escaping in http:// and in closing tags (if I think I can get away with a regex to match xhtml)
I think they went with the flags like that because it is the same in other regular expression implementations. But you're probably right, if it was redesigned, perhaps it'd be better to use it's own parameter.
|
8

Simplest method:

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

1 Comment

Duplicate of Daniel Vandersluis answer.
3

John was the first and I think his solution should be preferred, because it's way more elegant, however here is another one:

$site = implode("/", array_filter(explode("/", $site)));

Update

Thx. I updated it and now even handles things like this

$site = "///test///test//"; /* to => test/test */

Which probably makes it even cooler than the accepted answer ;)

1 Comment

-1 uh, that shouldn't be a solution - and isn't one. your code returns $site as-is (i even tested it).
2

Is that what You want?

$url = 'http://www.example.com/';

if (substr($url, -1) == '/')
    $url = substr($url, 0, -1);

Comments

1

You could have a slash before the question mark sign and you could have the "?" sign or "/" in the parameters (.com/?price=1), so you shouldn't delete this every time. You need to delete only the first slash "/" before the question mark "?" or delete the last slash "/" if you have no question marks "?" at all.

For example:

https://money.yandex.ru/to/410011131033942/?&question=where?&word=why?&back_url=https://money.yandex.ru/to/410011131033942/&price=1

would be

https://money.yandex.ru/to/410011131033942?&question=where?&word=why?&back_url=https://money.yandex.ru/to/410011131033942/&price=1

And

https://money.yandex.ru/to/410011131033942/

would be

https://money.yandex.ru/to/410011131033942

The PHP code for this would be:

if(stripos($url, '?')) {
    $url = preg_replace('{/\?}', '?', $url);
} else {
    $url = preg_replace('{/$}', '', $url);
}

Comments

0

The most elegant solution is to use rtrim().

$url = 'http://www.domain.com/';

$urlWithoutTrailingSlash = rtrim($url, '/');

EDIT

I forgot about rtrim();

You could also play around parse_url().

1 Comment

Wow, there are a lot of solutions to this problem. Thanks!
-1
$new_string = preg_replace('|/$|', '', $string);

1 Comment

Using regex for such a simple task is overkill. What's wrong with rtrim()?
-2

Perhaps a better solution would be to use .htaccess, but php can also do it with something like this:

<?php
    header('location: '.preg_replace("/\/$/","",$_SERVER['REQUEST_URI']));
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.