Page 1 of 1
[SOLVED] Regex Question?
Posted: Mon May 02, 2005 9:17 pm
by Sphen001
Hi,
I have a string that contains a filename, ie. index.php. I need to get a way to remove the .php from the filename. Can anyone suggest a way to get the name without the extension. Also, the filename can be different, but it will always end with .php.
Thanks for your help
Sphen001
Posted: Mon May 02, 2005 10:10 pm
by hanji
Give this a shot. It checks to see if it ends with .php and does a substring to parse the name. There's a bunch of ways to do this... but this is one option.
HTH
hanji
Code: Select all
<?
$fileName = "index.php";
if(eregi("\.php$",$fileName)){
$fileName = substr($fileName,0,-4);
}
echo $fileName;
?>
Posted: Tue May 03, 2005 8:09 am
by Sphen001
Ok, thanks a lot. I was thinking about something like this, but I was unsure how to go about and implement it.
Thanks again
Sphen001
Posted: Tue May 03, 2005 8:12 am
by Chris Corbyn
Code: Select all
$filename = 'somefile.php';
$basename = preg_replace('/\b(.*?)\.php\b/i', "$1", $filename);
Posted: Tue May 03, 2005 9:18 am
by Sphen001
Thanks d11wtq
If you don't mind, could you explain how this regex works? I'm trying to learn how to do them myself, and I think real examples would be helpful. I've looked at several resources, and I'm starting to get the hang of it, but this would be really great.
Thanks again,
Sphen001
Posted: Tue May 03, 2005 10:28 am
by hanji
Code: Select all
$basename = preg_replace('/\b(.*?)\.php\b/i', "$1", $filename);
nice!
hanji
Posted: Tue May 03, 2005 2:26 pm
by visionmaster
Hi,
Indeed, RegExps are very mighty, but they are also WORN (Write Once Read Never)
I'll try to explain the expression:
'/\b(.*?)\.php\b/i', "$1",
( )
Match the following regular expression and capture its match into backreference number 1:
(So $1 stores the captured match.)
(.*?)
Match any single character that is not a line break character, between zero and unlimited times, as few times as possible.
\.
Match the charachter "." literally.
php
Match the charachter "php" literally.
Posted: Tue May 03, 2005 3:01 pm
by Todd_Z
Code: Select all
$file = "index.php";
$parts = explode( ".", $file );
echo "Filename: ".$parts[0];
echo "<br>Extension: ".$parts[1];
Posted: Tue May 03, 2005 3:30 pm
by Sphen001
Thanks everyone for your help.
I will try it a few different ways and see which is best. Also thanks visionmaster for your explanation. It was very helpful.
Thanks again
Sphen001
Posted: Thu May 05, 2005 4:07 am
by vigge89
Todd_Z wrote:Code: Select all
$file = "index.php";
$parts = explode( ".", $file );
echo "Filename: ".$parts[0];
echo "<br>Extension: ".$parts[1];
I don't reccomend using that code, as a filename could contain more than just one full stop.
basename() can also be used if you just want to delete a single defined extension.
Regular expression for deleting any extension from the filename:
Code: Select all
$filename = 'somefile.php';
$basename = preg_replace('#([^\\\/]*?)(\.([a-zA-Z0-9]+))$', '\\1', $filename);
Not tried, but it should work

(\\1 contains filename, \\3 extension).