Junodixon Posted November 19, 2008 Share Posted November 19, 2008 I need some help with preg_replace. I have some text that looks like this ./files/pdf/something.pdf and would like the replace it with something.pdf So how would I remove ./files/pdf/ Thanks Junodixon Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/ Share on other sites More sharing options...
flyhoney Posted November 19, 2008 Share Posted November 19, 2008 probably done easier using pathinfo(). http://us2.php.net/pathinfo <?php $path_parts = pathinfo('./files/pdf/something.pdf'); echo $path_parts['basename']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-693832 Share on other sites More sharing options...
DarkWater Posted November 19, 2008 Share Posted November 19, 2008 Even easier: $path = "./files/pdf/something.pdf"); $filename = basename($path); Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-693843 Share on other sites More sharing options...
Junodixon Posted November 19, 2008 Author Share Posted November 19, 2008 Thats not really what I trying to do. I have a list of them like ./files/pdf/something.pdf ./files/pdf/something1.pdf ./files/pdf/something2.pdf ./files/pdf/something3.pdf and they are all set up in a variable called $files and im trying to remove the ./files/pdf/ from everyone of them. Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-693862 Share on other sites More sharing options...
darkfreaks Posted November 19, 2008 Share Posted November 19, 2008 you want mod_rewrite in htaccess then that will rewrite the url Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-693883 Share on other sites More sharing options...
DarkWater Posted November 19, 2008 Share Posted November 19, 2008 Thats not really what I trying to do. I have a list of them like ./files/pdf/something.pdf ./files/pdf/something1.pdf ./files/pdf/something2.pdf ./files/pdf/something3.pdf and they are all set up in a variable called $files and im trying to remove the ./files/pdf/ from everyone of them. How about: <?php $files = array('./files/pdf/something.pdf', './files/pdf/something1.pdf', './files/pdf/something2.pdf', './files/pdf/something3.pdf'); $files = array_map(create_function('$x', 'return basename($x);'), $files); print_r($files); Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-693890 Share on other sites More sharing options...
Junodixon Posted November 20, 2008 Author Share Posted November 20, 2008 Its closer but still not what I'm trying to do. Maybe I'm not explaining it right. Here is what I'm doing. I have a page set up where someone can go and select file they want. It will zip the files and make a link that expires in 24 hours. It then takes that link and emails it to them along with a list of files they requested. I'm trying to get it to list the files in that email. I have got it to list out the files like this. ./files/pdf/something.pdf ./files/pdf/something2.pdf ./files/pdf/something3.pdf I just want to remove the ./files/pdf/ I have used preg_match to remove the . files and pdf but I still have the //. If I know how to remove the // I would be set. Maybe that will help a little. Thanks Junodixon Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-694461 Share on other sites More sharing options...
premiso Posted November 20, 2008 Share Posted November 20, 2008 The basename should do that for you. But another example would be: <?php $filename = "./files/pdf/something.pdf"; $filename = explode("/", $filename); $filename = $filename[count($filename)-1]; echo $filename; ?> As said above basename should do that without the fuss. Quote Link to comment https://forums.phpfreaks.com/topic/133402-preg_replace-help/#findComment-694636 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.