Chuck Norris Chuck Norris - 3 months ago 8
PHP Question

Symfony ExpressionLanguage evaluate string with dashes

I'm trying to evaluate some strings containing dashes with the symfony ExpressionLanguage component.

Here is what I've got so far :

...
$string = 'user.chuck-norris.getId()';

$language = new ExpressionLanguage();
$evaluated = $language->evaluate($expression, $users);
...


This returns me the following error :

Variable "norris" is not valid around position 12. (Symfony\Component\ExpressionLanguage\SyntaxError)


If I change the dash "-" by an underscore "_", this works, but I have slug system which use dash and I dont wont to change it if I can avoid it.

Is there any solution?

Thanks

Answer

Like stated by Yonel, dashes are interpretated as operator.

So for this to work, I just have to replace dashes by undescores

$string = 'user.chuck-norris.getId()';

And then before making the request, replace _ by -

$value = str_replace('_', '-', $value);