2

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

Chuck Norris
  • 1,125
  • 1
  • 12
  • 28
  • 2
    The dashes are interpretated as arithmetic operator (subtraction) http://symfony.com/doc/current/components/expression_language/syntax.html#arithmetic-operators I think that you unique solution is convert it to underscore. – yceruto Aug 25 '16 at 12:00
  • Yes, just saw this. I have to add quotes (or double quotes) to the string to make it work like I wank. $string = '"user.chuck-norris.getId()"'; Thanks. :) – Chuck Norris Aug 25 '16 at 12:03

1 Answers1

2

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);
Chuck Norris
  • 1,125
  • 1
  • 12
  • 28