Get substrings from a string containing a namespaced static method call [closed]
Asked Answered
S

1

0

I am stuck with a regular expression.

$matches    = array();
// $controller = $this->getRequest()->attributes->get('_controller');
$controller = "Acme\MyBundle\Controller\MyController::myAction";
preg_match('/(.*)\\\Bundle\\\(.*)\\\Controller\\\(.*)Controller::(.*)Action/', $controller, $matches);

print_r($matches);

Returns (see example)

Array
(
)

Expected result

Array
(
    [0] => Acme\MyBundle\Controller\MyController::myAction
    [1] => Acme
    [2] => My
    [3] => My
    [4] => my
)

Anyone can help? This regexp seems to be legit, maybe it's just a problem with the backslashes? I tried around but didn't get it.

Schulze answered 4/4, 2014 at 4:11 Comment(3)
What are you trying to match ?Boxfish
It doesn't match \MyBundle\ part, since you only have \Bundle\ in your regex.Settlement
@ShankarDamodaran uh yeah sorry, forgot to detail that, will update.Schulze
G
2

Please try below expression. Is it expected? Or tell me your exact requirements.

<?php

$matches    = array();
// $controller = $this->getRequest()->attributes->get('_controller');
$controller = "Acme\MyBundle\Controller\MyController::myAction";
preg_match('/(.*)\\\(.*)Bundle\\\Controller\\\(.*)Controller::(.*)Action/', $controller, $matches);

print_r($matches);
Gandhi answered 4/4, 2014 at 4:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.