I dont really understand xP
Cant you use split() ?
Matching fixed URI's
Moderator: General Moderators
-
alex.barylski
- DevNet Evangelist
- Posts: 6267
- Joined: Tue Dec 21, 2004 5:00 pm
- Location: Winnipeg
Re: Matching fixed URI's
I think I have my regex figured out, finally 
URI = food/apples.html
Results in
However one nagging fact I would like to fix is the fact if I append a query string to the end, like:
URI = food/apples.html?name=value&name=value
The results are then
Ideally the last element would *not* contain the '?' but the catch is it needs to be optional
Any ideas?
Code: Select all
#^([^/]+)/([^\.|/]+)\.([^\?|/]+)(.*)#Results in
Code: Select all
[0] = food
[1] = apples
[2] = htmlURI = food/apples.html?name=value&name=value
The results are then
Code: Select all
[0] = food
[1] = apples
[2] = html
[3] = ?name=value&name=value
Any ideas?
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Matching fixed URI's
Why do you think the '?' should NOT be included? You're matching your entire string, so the '?' is a part of it (thus it ends up in your $matches). You should split on some delimiters instead so they are "removed" from your string.PCSpectra wrote:Update: I have tried the following regex:
It works but STILL includes the ? or / in the trailing group, which is what I want to ideally leave out of the final group, but the ? or / and everything after the main URI MUST be optional...Code: Select all
#([^/]+)/([^\?|/]+)([^\?|/]{0,}.*)#
What am I doing wrong?
Here's a slight modification on my previous suggestion (which worked fine up until you posted a different format of your url's, btw):
Code: Select all
<?php
$tests = array(
"/food/apples/",
"food/apples?name=value&name=value",
"food/apples/index(10).html",
"food/apples/index.html"
);
$delimiters = "/().?";
foreach($tests as $t) {
if(preg_match('#^/?([^/]*/)*.*$#', $t)) {
print_r(preg_split("#[{$delimiters}]+#", preg_replace('#^/|/$#', '', $t)));
}
}
?>Good luck!
Re: Matching fixed URI's
Nice one prometheuzz, my rexexp $tests got longer and longer 
- prometheuzz
- Forum Regular
- Posts: 779
- Joined: Fri Apr 04, 2008 5:51 am
Re: Matching fixed URI's
Thanks!papa wrote:Nice one prometheuzz, my rexexp $tests got longer and longer
; )
Re: Matching fixed URI's
Deleted 
Didnt realise it had been answered already
Didnt realise it had been answered already