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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.