trq Posted September 4, 2007 Share Posted September 4, 2007 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 More sharing options...
trq Posted September 4, 2007 Author Share Posted September 4, 2007 Also Ide like to validate that each part is only a-zA-Z0-9 and _ Thanks. Link to comment https://forums.phpfreaks.com/topic/67889-solved-help-matching-a-path/#findComment-341244 Share on other sites More sharing options...
effigy Posted September 4, 2007 Share Posted September 4, 2007 <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 https://forums.phpfreaks.com/topic/67889-solved-help-matching-a-path/#findComment-341300 Share on other sites More sharing options...
trq Posted September 4, 2007 Author Share Posted September 4, 2007 Oh man, I was hoping you might take a look at it for me. I'm not at my machines so can't test it untill the moring, but thanks. Link to comment https://forums.phpfreaks.com/topic/67889-solved-help-matching-a-path/#findComment-341311 Share on other sites More sharing options...
trq Posted September 5, 2007 Author Share Posted September 5, 2007 Thanks mate, tested and working exactly how I need. Link to comment https://forums.phpfreaks.com/topic/67889-solved-help-matching-a-path/#findComment-341725 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.