LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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
 
LinkBack Search this Thread
Old 05-22-2007, 07:54 AM   #1
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Rep: Reputation: 30
Question Regular Expression Question


I'm trying to figure out how to use a regular expression with PHP to do the following:

I would like to take a filepath and remove the filename up to the last folder. Example:

Code:
Radio Queue/Hootie & the Blowfish/Musical Chairs/01 I Will Wait.mp3
I would like to cut out
Code:
01 I Will Wait.mp3
so that all I have left is
Code:
Radio Queue/Hootie & the Blowfish/Musical Chairs/
There may be more or less folders, so basically I just need something to remove from the end to the last /.

Thanks,
Adam
 
Old 05-22-2007, 07:59 AM   #2
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
Try something like,

Code:
/^.*([^\/]+)$/
and the group is what you're looking for.

Use this funtion : http://us2.php.net/manual/en/function.preg-match.php

Last edited by krizzz; 05-22-2007 at 08:06 AM.
 
Old 05-22-2007, 08:34 AM   #3
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks krizzz, which PHP preg expression would work the best with this? I though match, but that only counts the number of times it matches, right?
 
Old 05-22-2007, 12:00 PM   #4
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
No, preg_match returns also the group matches through the output parameter. You can call it like that :

Code:
$num = preg_match ("/^.*([^\/]+)$/", $line , $matches);
in this case, you should find your filename in $matches[1] . If you had more groups indicated by parentheses then they would be returned in the corresponding $matches array elements.
 
Old 05-22-2007, 01:02 PM   #5
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Thanks for clearing that up. I'll give that a shot.
 
Old 05-22-2007, 02:06 PM   #6
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
It doesn't seem to grab it. Here is my code:

PHP Code:
$num preg_match ("/^.*([^\/]+)$/"$cover $matches);
echo 
$cover;
echo 
$matches[1]; 
This is the output:
Code:
Radio Queue/Hootie & the Blowfish/Musical Chairs/01 I Will Wait.mp33
It appears to only be grabbing the number 3 at the end.
 
Old 05-22-2007, 02:07 PM   #7
azwebco
LQ Newbie
 
Registered: Jun 2006
Posts: 6

Rep: Reputation: 0
for this kind of problem you're probably better served with using strrpos(). It's faster, simpler and less resource intensive. Find the last occurence and then cut the substr() to that length.
 
Old 05-22-2007, 02:50 PM   #8
windisch
Member
 
Registered: Nov 2004
Location: Gahanna, Ohio, USA
Distribution: Fedora 9
Posts: 158

Original Poster
Rep: Reputation: 30
Wow, that was easy with that command. Thanks azwebco. And thanks again for your help krizzz.
 
Old 05-22-2007, 03:27 PM   #9
krizzz
Member
 
Registered: Oct 2004
Location: NY
Distribution: Slackware
Posts: 198

Rep: Reputation: 30
Yeah, it's definitely the most efficient way to do it with php. There's no need to use regexp in such a simple situations. Some other quite efficient way is :

Code:
array_pop(split('/',$cover));
 
  


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expression question gauge73 Linux - General 5 10-28-2005 11:33 AM
Regular expression question gauge73 Linux - General 2 10-28-2005 09:32 AM
regular expression question zero79 Linux - Software 1 07-11-2005 07:03 PM
Regular expression question - John Sloan Linux - Software 1 09-08-2004 12:33 PM
regular expression question Gantrep Linux - Software 2 04-20-2003 04:24 PM


All times are GMT -5. The time now is 12:14 AM.

Main Menu
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