Search code examples
modxmodx-revolution

php strips [[:char_class:]] from the string


When concatenating mysql regex character classes in php they disappear from the resulting string i.e.:

$regexp_arr = array('(word1)', '(word2)');
$value = 'word3';
$regexp_str = implode('[[:space:]]', $regexp_arr);
$v1 = '[[:<:]](' . $value . ')';

echo $regexp_str;
// gives
'(word1)(word2)';
// instead of 
'(word1)[[:space:]](word2)'

echo $v1;
// gives
'(word3)' 
//instead of 
'[[:<:]](word3)'

I've tried with double quotation marks ", the result still the same.

Is there a special way to concatenate this in php? Why are the '[[:char_class:]]' getting stripped?

server php version is 5.6.36


Solution

  • In MODX, [[ and ]] are special characters used to indicate they are tags MODX needs to process. Even when you echo or retrieve it from the database, MODX will process them when rendering.

    For debugging, you can follow-up your echo with an exit().

    echo $regexp_str;
    exit();
    

    That short-circuits MODX and gives you the actual value of the string including the square brackets.

    If you want the value to be visible in a MODX-rendered resource or template, then you'll have to replace them with their html entities first:

    $regexp_str = str_replace(['[',']'], ['&#91;', '&#93;'], $regexp_str);