Jump to content

removing everything after last _ in a string


stijnvb

Recommended Posts

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!

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.