Re: Matching fixed URI's
Posted: Tue Jan 27, 2009 8:51 pm
I dont really understand xP
Cant you use split() ?
Cant you use split() ?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
#^([^/]+)/([^\.|/]+)\.([^\?|/]+)(.*)#Code: Select all
[0] = food
[1] = apples
[2] = htmlCode: Select all
[0] = food
[1] = apples
[2] = html
[3] = ?name=value&name=value
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?
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)));
}
}
?>Thanks!papa wrote:Nice one prometheuzz, my rexexp $tests got longer and longer