Forum Moderators: coopster

Message Too Old, No Replies

preg replace beginning and end of file name

         

RammsteinNicCage

3:18 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



I have a filename such as admin_press.php. I want to do a preg_replace so that I end up with just "press". I can get it to remove "admin_" or ".php", but never together. How would I go about doing that?

This is what I was hoping would work, but it removes everything.

preg_replace("/^admin_.*?\.php$/", "", $file)

eelixduppy

3:27 pm on Apr 7, 2007 (gmt 0)



Try something like this:

$new_name = preg_replace("/^admin_(.+)\.php$/", "\\1", $file);

You have to group the part you want to keep and add it to the replacement text.

RammsteinNicCage

4:25 pm on Apr 7, 2007 (gmt 0)

10+ Year Member



Perfect, thanks!