LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 07-28-2003, 05:20 AM   #1
slizadel
LQ Newbie
 
Registered: Jun 2003
Distribution: RH9, Gentoo, Slack
Posts: 23

Rep: Reputation: 15
Regular Expression


For all you regular expression gurus:

I'm using PHP version 4.3.2 with Apache 1.3.27.
What I am trying to do is obtain the name of the file that the user is currently viewing. For example, if a user is at http://foo.com/bar/foo.php I would like to obtain foo.php. As far as I can tell, the only way to do this is to use a regular expression to retrieve this information from the PHP _SERVER variables. In this instance, _SERVER["PHP_SELF"] returns /bar/foo.php. What I am looking for is a regular expression to return whatever is after the last '/', which would be the desired foo.php string.

If anyone can help me out it would be greatly appreciated.
 
Old 07-28-2003, 05:53 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
Code:
preg_match_all('/\/(\w+\.php)/', $php_self, $filename);
print $filename[1][0];
it's ugly but it does work, i'm sure there's a much better way to do it though, as you're only after one element.so an array seems redundant.
 
Old 07-28-2003, 06:04 AM   #3
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
OR you could use preg_replace to remove the bit you don't want rather than getting the bit you actually want:
Code:
print preg_replace("/^.*\/(\w+\.php)/", $1, $php_self)
I do get the feelijng that there is already a variable with that in it though.
 
Old 07-28-2003, 06:08 AM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,415

Rep: Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968Reputation: 1968
and, of course, don't quote me on the regex's, it think they're right but i can't test them.
 
Old 07-28-2003, 06:16 AM   #5
slizadel
LQ Newbie
 
Registered: Jun 2003
Distribution: RH9, Gentoo, Slack
Posts: 23

Original Poster
Rep: Reputation: 15
acid,

Thanks for the regex. It worked great. I guess maybe I should look into purchasin this for future reference. (or maybe just use it as an excuse to add to my O'reilly collection.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with regular expression aecaudel Programming 6 11-04-2005 06:28 AM
Need help with Regular Expression subaruwrx Linux - Newbie 6 09-04-2004 08:48 PM
Anyone know regular expression? ahhua Linux - Software 1 12-04-2003 09:13 AM
Regular Expression Help WeNdeL Linux - General 1 08-14-2003 11:08 AM
regular expression gumby Programming 3 07-15-2003 01:13 PM


All times are GMT -5. The time now is 06:53 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration