Pro.Luv Posted December 10, 2008 Share Posted December 10, 2008 Hey, I want to take a URL for example: http://www.website.com/ I want to replace the last "/" so that it's like.. http://www.website.com But I need to first check if the last "/" exists because not all the URL's I'm parsing has the slash. I'm not really any good with Regex I appreciate any help Thanks Link to comment https://forums.phpfreaks.com/topic/136342-replace/ Share on other sites More sharing options...
DarkWater Posted December 10, 2008 Share Posted December 10, 2008 You could do this without a regex, but here: <?php $string = 'http://www.website.com/'; $string = preg_replace('~/$~', '', $string); echo $string; ?> Link to comment https://forums.phpfreaks.com/topic/136342-replace/#findComment-711297 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 rtrim($string,'/'); If it's there, it will remove it. Link to comment https://forums.phpfreaks.com/topic/136342-replace/#findComment-711427 Share on other sites More sharing options...
phpSensei Posted December 10, 2008 Share Posted December 10, 2008 $url= substr("http://www.google.com/", 0, -1); // returns http://www.google.com Link to comment https://forums.phpfreaks.com/topic/136342-replace/#findComment-711967 Share on other sites More sharing options...
DarkWater Posted December 12, 2008 Share Posted December 12, 2008 $url= substr("http://www.google.com/", 0, -1); // returns http://www.google.com That only works if there's actually a slash on the end. It doesn't do any detection... CV's solution is the best. Nice one. Link to comment https://forums.phpfreaks.com/topic/136342-replace/#findComment-713361 Share on other sites More sharing options...
nrg_alpha Posted December 12, 2008 Share Posted December 12, 2008 CV's solution is the best. Nice one. I agree.. sometimes, the simplest solution is so elegant it isn't funny (or perhaps it is, depending on your point of view). Link to comment https://forums.phpfreaks.com/topic/136342-replace/#findComment-713472 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.