tags:

views:

997

answers:

4

Hello,

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?

Thanks in advance,

John

$remove_array = array('http://www.', 'http://', 'https://', 'https://www.', 'www.');
$site = str_replace($remove_array, "", $_POST['site']);
+2  A: 
$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.

John Kugelman
I thought the matching chars had to be the same, i.e. { and { but not { and }
alex
Also, the chars are useful when you want to supply flags. e.g. /[a-z]*/i means match everything in a case insensitive way.
alex
(), {}, [], 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.
John Kugelman
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)
alex
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.
alex
Thanks for the response.
The delimiters are there because it's called preg_*, meaning that the PHP regex library is based on perl regular expressions (or at least perl-compatible). In perl, one doesn't have to quote the expression: `$a =~ s/foo/bar/;` or `if ($myVar =~ m#/$#)`
Thomas G. Mayfield
+1  A: 

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

merkuro
Cool. Thanks for the response.
-1 uh, that shouldn't be a solution - and isn't one. your code returns $site as-is (i even tested it).
Schnalle
@Schnalle thx for the input. Updated it!
merkuro
+11  A: 

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, "/");
Daniel Vandersluis
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");
Schnalle
also, rtrim removes all trailing slashes $site = 'http://foo.bar//////", while the regexp only removes one trailing slash.
Schnalle
A: 

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().

alex
Wow, there are a lot of solutions to this problem. Thanks!