Jump to content

[SOLVED] Help matching a path.


trq

Recommended Posts

I've never been much good with these regex things.

 

#!/usr/bin/php
<?php

class request {

    public $app;
    public $method;
    public $args;

    public function parse_uri($uri) {
        if (preg_match("|/(.*?)/(.*?)/(.*)|",$uri,$return)) {
            if (is_array($return)) {
                $this->app      = isset($return[1]) ? $return[1]                    : false;
                $this->method   = isset($return[2]) ? $return[2]                    : false;
                $this->args     = isset($return[3]) ? explode('/',$return[3])       : false;
                return true;
            }
        }
        return false;

    }

}

$rq = new request();
if ($rq->parse_uri('/app/method/a1/a2')) {
    print_r($rq);

}

?>

 

produces....

 

request Object
(
    [app] => app
    [method] => method
    [args] => Array
        (
            [0] => a1
            [1] => a2
        )

)

 

As expected. But it wont match when I pass it....

 

<?php

$rq = new request();
if ($rq->parse_uri('/app')) {
    print_r($rq);

}

 

I get nothing. Can anyone help?

Link to comment
Share on other sites

<pre>
<?php
$tests = array(
	'/app',
	'/app/method',
	'/APP/METHOD',
	'/app/method/a1',
	'/app/method/a1/a2',
	'/app/method/a1/a2/a3',
	'method/a1',
	'/*/method/a1'
);
foreach ($tests as $test) {
	echo "<b>$test</b><br>";
	preg_match('!
		\A ### BOL
		/(\w+) ### App
		(?:/(\w+))? ### Method
		((?:/\w+)+)? ### Args
		\z ### EOL
	!x', $test, $matches);
	if (isset($matches[3])) {
		$matches[3] = preg_split('#/#', $matches[3], -1, PREG_SPLIT_NO_EMPTY);
	}
	print_r($matches);
	echo '<hr>';
}

?>
</pre>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.