Jump to content

[SOLVED] Regex backreference: use as array index?


tox_yray

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.

Archived

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

×
×
  • Create New...

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.