stijnvb Posted February 13, 2011 Share Posted February 13, 2011 What I want to do is simple: The string "this_is_an_example.php" should return "this_is_an_" while: "this_is_an_example_filename.php should return "this_is_an_example_" Is there a simple function which can cut off the part after the last _ ? I have been looking and trying several functions, but can't seem to find the right one for this. (the last _ being cut off of course wouldn't be a problem I couldn't live with :-) ) Any help will be greatly appreciated! Link to comment https://forums.phpfreaks.com/topic/227575-removing-everything-after-last-_-in-a-string/ Share on other sites More sharing options...
stijnvb Posted February 13, 2011 Author Share Posted February 13, 2011 I could probably use explode and put everything together except the last part ... but I'm pretty sure there's a built-in function for this ... Link to comment https://forums.phpfreaks.com/topic/227575-removing-everything-after-last-_-in-a-string/#findComment-1173827 Share on other sites More sharing options...
kenrbnsn Posted February 13, 2011 Share Posted February 13, 2011 Here's one way of doing this: <?php function tester($str,$delim='_') { $tmp = explode($delim,$str); $dmy = array_pop($tmp); return (implode($delim,$tmp) . $delim); } echo tester("this_is_an_example_filename.php"); ?> Ken Link to comment https://forums.phpfreaks.com/topic/227575-removing-everything-after-last-_-in-a-string/#findComment-1173829 Share on other sites More sharing options...
stijnvb Posted February 13, 2011 Author Share Posted February 13, 2011 Ken, You offered me more than I asked! Does exactly what I needed! It's not a built in function, but don't think that really matters if I don't even have to write it myself!!! Thanks! Stijn Link to comment https://forums.phpfreaks.com/topic/227575-removing-everything-after-last-_-in-a-string/#findComment-1173833 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.