2

I have the follow code, which get $_GET and make division by controller, action and params

http://localhost/controller/action/param1/param2

$url = explode('/', $_GET['url']);
$urlSize = count($url);
$filter = $this->factory('Core\Security\Filter');

if($urlSize >= 1) {
    $controller = $filter->replace($url[0], 'friendly-url');

    if($urlSize >= 2) {
        $action = $filter->replace($url[1], 'friendly-url');

        if($urlSize >= 3) {
            unset($url[0], $url[1]);

            foreach($url as $index => $param) {
                $params[] = $filter->replace($param, 'friendly-url');
            }
        }
    }
}

Core\Security\Filter->replace() which I am developing right now:

public function replace($data = null, $type = 'alphanumeric') {
    /*
        @TODO, implement regex by type

        numeric
                $regex = '/^[[:digit:]]$/';
        alphanumeric
                $regex = '/^[[:alnum:]]$/';
        friendly-url
                $regex = '/[^a-zA-Z0-9_-]+/';
                $replace = '-'
        username
                $regex = '/^[^a-zA-Z0-9_-.]{3,32}+';
        email
                $regex = '/^[[a-zA-Z0-9_-.]{1,96}]+@[a-zA-Z0-9-]{2,64}+(?:\.[a-zA-Z0-9-]+)*$/';
    */
}

Ok, the problem I have is: How to get urls with this format:

http://localhost/controller/action/param1/param2:value2

$param array:

Array(
    [0] => param1
    [1] => Array(
       [param2] => value2
    )
)

SOLVED WITH THIS:

foreach($url as $index => $param) {
    if(strstr($param, ':')) {
        $subParam = explode(':', $param, 2);
        $this->_request['params'][][$subParam[0]] = $filter->replace($subParam[1], 'friendly-url-param');
    } else {
        $this->_request['params'][] = $filter->replace($param, 'friendly-url-param');
    }
}
Gabriel Santos
  • 4,934
  • 2
  • 43
  • 74
  • 1
    Pretty please with sugar on top use `filter_var($str, FILTER_VALIDATE_EMAIL)` instead of a regex that does not match a whole bunch of valid email addresses, a subject that has been [covered](http://stackoverflow.com/questions/201323) [here](http://stackoverflow.com/questions/1903356) [at](http://stackoverflow.com/questions/703060) [length](http://stackoverflow.com/questions/997078) – DaveRandom May 30 '12 at 23:21
  • 1
    Be warned: colons in URIs are [disallowed in windows apache](https://issues.apache.org/bugzilla/show_bug.cgi?id=41441). It's best to avoid colons in URLs in PHP applications as a result. – Frank Farmer May 30 '12 at 23:42
  • One of requirements are not Windows servers ;) – Gabriel Santos May 30 '12 at 23:48
  • @DaveRandom this case, `FILTER_SANITIZE_EMAIL` because, I want the sanitized e-mail, not to check if is valid or not. – Gabriel Santos May 30 '12 at 23:53

1 Answers1

2
<?php

$url = 'http://localhost/controller/action/param1/param2:value2';

$parts = parse_url($url);
$path = $parts['path'];

list($controller, $action, $params) = explode('/', ltrim($path, '/'), 3);

function parse_params($params) {
    $parsed_params = array();
    $path_segments = explode('/', $params);
    foreach ($path_segments as $path_segment) {
        if (strstr($path_segment, ':')) {
            list($k, $v) = explode(':', $path_segment, 2);
            $parsed_params[] = array($k => $v);
        } else {
            $parsed_params[] = $path_segment;
        }
    }
    return $parsed_params;
}



$params = parse_params($params);

print_r($params);

OUTPUT

Array
(
    [0] => param1
    [1] => Array
        (
            [param2] => value2
        )

)
sberry
  • 128,281
  • 18
  • 138
  • 165
  • Probably REGEX does not do this job.. But, I have solved my problem with my updated question, based on your answer. Thanks! – Gabriel Santos May 30 '12 at 23:36