Skip to content

[ExpressionLanguage] Add caution about backslash handling #6241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 7, 2016
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Add a caution about backslash escaping
  • Loading branch information
wouterj committed Feb 7, 2016
commit 31d74e5d2577436e27f765bafb594678bb1348d0
34 changes: 12 additions & 22 deletions components/expression_language/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@ The component supports:
* **booleans** - ``true`` and ``false``
* **null** - ``null``

.. caution::

A backslash (``\``) must be escaped by 4 backslashes (``\\\\``) in a string
and 8 backslashes (``\\\\\\\\``) in a regex::

echo $language->evaluate('"\\\\"'); // prints \
$language->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"'); // returns true

Control characters (e.g. ``\n``) in expressions are replaced with
whitespace. To avoid this, escape the sequence with a single backslash
(e.g. ``\\n``).

.. _component-expression-objects:

Working with Objects
Expand Down Expand Up @@ -181,28 +193,6 @@ Comparison Operators
You must use parenthesis because the unary operator ``not`` has precedence
over the binary operator ``matches``.

A backslash(``\``) must be escaped by 4 backslashes(``\\\\``) in a string and
8 backslashes(``\\\\\\\\``) in a regex::

$language->evaluate('"\\\\"');
// returns \

$language->evaluate('"a\\\\b" matches "/^a\\\\\\\\b$/"');
// returns true

Control characters must be defined as the escaped form of their escape sequences.
Otherwise, they will be replaced by spaces and ignored::

$language->evaluate('"a\nb"');
// returns a b

$language->evaluate('"a\\nb"');
// returns a\nb

This is because the backslashes in a string will be stripped by the
``stripcslashes()`` function and the stripped slashes in a regex will be
stripped again by the regex engine.

Examples::

$ret1 = $language->evaluate(
Expand Down