[SOLVED] Problem with regex and backreference
Posted: Mon Dec 20, 2010 1:42 am
Hi All,
I'm trying to use the backreference \\1 or \${1} in a preg_replace, however I'm not able to get it right. Here's an example of what I want to accomplish:
Basically what I'm trying to do is check if $mytest->data['TEST'] has been set. If so it should print it's value, if not then it prints an empty string. However it always prints an empty string.
Hope some one can help me out.
I'm trying to use the backreference \\1 or \${1} in a preg_replace, however I'm not able to get it right. Here's an example of what I want to accomplish:
Code: Select all
$mytest->data['TEST'] = 'This is a regular test';
$text = '[TEST] based on my findings';
$new_text = preg_replace('/\[([^0-9](?:[A-Z0-9\-_]+))\]/', (isset($mytest->data['\\1']) ? $mytest->data['\\1'] : '') , $text);
print_r($new_text);
Hope some one can help me out.