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
https://forums.phpfreaks.com/topic/67889-solved-help-matching-a-path/
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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.