Welcome to WebmasterWorld Guest from 18.207.136.184

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

preg replace beginning and end of file name

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

Preferred Member

10+ Year Member

joined:May 20, 2003
posts:493
votes: 0


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)
3:27 pm on Apr 7, 2007 (gmt 0)

Senior Member

joined:Nov 12, 2005
posts:5967
votes: 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.

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

Preferred Member

10+ Year Member

joined:May 20, 2003
posts:493
votes: 0


Perfect, thanks!
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members