tox_yray Posted August 28, 2006 Share Posted August 28, 2006 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 https://forums.phpfreaks.com/topic/18950-solved-regex-backreference-use-as-array-index/ Share on other sites More sharing options...
effigy Posted August 28, 2006 Share Posted August 28, 2006 [code]echo preg_replace('/\$\$!!WIDTH(\d+)\+\+--/e', "\$lole[$1]", $STRING);[/code] Link to comment https://forums.phpfreaks.com/topic/18950-solved-regex-backreference-use-as-array-index/#findComment-81945 Share on other sites More sharing options...
tox_yray Posted August 29, 2006 Author Share Posted August 29, 2006 Works like a charm!!! Thanks a bunch! Link to comment https://forums.phpfreaks.com/topic/18950-solved-regex-backreference-use-as-array-index/#findComment-82233 Share on other sites More sharing options...
GameMusic Posted September 3, 2006 Share Posted September 3, 2006 Try preg_replace_callback too. Link to comment https://forums.phpfreaks.com/topic/18950-solved-regex-backreference-use-as-array-index/#findComment-85038 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.