Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how do i removes slashes from end of urls? 1

Status
Not open for further replies.
There are several ways to do it. One is:

Code:
<?php
$a = '[URL unfurl="true"]http://www.site.com/';[/URL]
$a = preg_replace ('@/$@', '', $a);
print $a;
?>





Want the best answers? Ask the best questions! TANSTAAFL!
 
Try this:

$new_url = eregi_replace("^(.*)/$", "\\1", $orig_url);
 
or avoiding regex

Code:
function cleanse ($url){
 if (substr($url, -1,1) === "/"){
  return substr($url, 0, strlen($url) - 1));
 } else {
  return $url;
 }  
}
 
I Agree with jpadie's solution. PHP manual clearly states that regex should be avoided unless absolutely necessary. In this case, a simple substr, with an if statement to check whether the last character is indeed a slash, seems to do it nicely. And I'm pretty sure performance wise its faster than a regex.

Star for you. jpadie

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Certainly is faster ! ;-)
Code:
[small]function cleanse ($url){
 if (substr($url, -1,1) === "/"){[/small]
  return substr[red][b]([/b][/red]$url, 0, strlen[green][b]([/b][/green]$url[green][b])[/b][/green] - 1[red][b])[/b][/red][blue][b])[/b][/blue];
[small] } else {
  return $url;
 }  
}[/small]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top