[SOLVED] Regex Question?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

[SOLVED] Regex Question?

Post 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 :D

Sphen001
Last edited by Sphen001 on Tue Jul 12, 2005 9:23 pm, edited 1 time in total.
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

Post 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;
?>
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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 :D

Sphen001
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Code: Select all

$filename = 'somefile.php';

$basename = preg_replace('/\b(.*?)\.php\b/i', "$1", $filename);
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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
User avatar
hanji
Forum Commoner
Posts: 46
Joined: Fri Apr 29, 2005 3:23 pm

Post by hanji »

Code: Select all

$basename = preg_replace('/\b(.*?)\.php\b/i', "$1", $filename);
nice!

hanji
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post 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.
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Code: Select all

$file = "index.php";
$parts = explode( ".", $file );
echo "Filename: ".$parts[0];
echo "<br>Extension: ".$parts[1];
Sphen001
Forum Contributor
Posts: 107
Joined: Thu Mar 10, 2005 12:24 pm
Location: Land of the Beaver

Post 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 :D

Sphen001
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post 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).
Post Reply