Jump to content

Archived

This topic is now archived and is closed to further replies.

tox_yray

[SOLVED] Regex backreference: use as array index?

Recommended Posts

Ok, here is the problem:

I have this little bit of code:

[code]<?php

$lole = array('rouge', 'vert', 'bleu');

$STRING = "Le ciel est $$!!WIDTH2++--";

echo preg_replace("/\\$\\$!!WIDTH(\\d+)\\+\\+--/", "\\1", $STRING);

?>[/code]

The Regex is proper as the script echoes "Le ciel est 2". However, I would like him to echo "Le ciel est bleu" (where "bleu" is the index 2 of array $lole... You get the picture...)

I tried many different solutions but none seem to work. Here is what I've tried:
[code]
echo preg_replace("/\\$\\$!!WIDTH(\\d+)\\+\\+--/", $lole["\\1"], $STRING);
echo preg_replace("/\\$\\$!!WIDTH(\\d+)\\+\\+--/", "$lole[\\1]", $STRING);
echo preg_replace("/\\$\\$!!WIDTH(\\d+)\\+\\+--/", $lole[\\1], $STRING);
[/code]

Thanks for any help you can provide,
Bruno M-A.

Share this post


Link to post
Share on other sites
[code]echo preg_replace('/\$\$!!WIDTH(\d+)\+\+--/e', "\$lole[$1]", $STRING);[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.