Hi!

I was wondering if it is possible to use the preg_replace match return value ($1-9) as the key of the array containing the replacement string.

Let me know if anyone knows a solution.

Best Regards,
Snt

    found it:
    jlyman at oregontech dot net
    18-Nov-2003 11:29
    I had some text with numbers that I wanted to use as array references in the replacement text- here's how it worked for me:

    $string="My name is [0]";
    $names=array("Bill","Bob","Joe");

    $string = preg_replace("/[(\d+)]/",$names[(int) ${1}],$string);

    echo $string;

    output should be "My name is Bill"...

    Took me just a bit of playing around to figure this out (newbie), so I thought I would post it here.

      it still doesn't work.. this is my code:

      				$search = array (		'/\{OPTIONS_OBJ_(\d*)\}/Ui',
      								'/\{OPTIONS_TEXT_(\d*)\}/Ui',
      		'/\{OPTIONS_POINTS_(\d*)\}/Ui',
      		'/\{MENU_OBJ\}/Ui',
      		'/\{MENU\}/Ui',
      		'/\{QUESTION_NO\}/Ui',
      		'/\{QUESTION_TEXT\}/Ui',
      		'/\{TEST_ID\}/Ui',
      		'/\{TEST_NAME\}/Ui',
      		'/\{TEST_DESCRIPTION\}/Ui',
      		'/\{TEST_STATUS\}/Ui',
      		'/\{TEST_FILENAME\}/Ui'
      			);
      								$replace = array ( 		$options["obj"][(int) ${1}],
      						$options["text"][(int) ${1}],
      	$options["points"][(int) ${1}],
      	$options["menu_objname"],
      	$options["menu"],
      	$qn,
      	$question["title"],
      	$test_data["id"],
      	$test_data["title"],
      	$test_data["description"],
      	$test_data["status"],
      	$test_data["filename"]
      	);
      								$question_html .= preg_replace($search,$replace,$question["design"]);

      It seems the (int) ${1} reference doesn't work in search/replace arrays.

        Found it:

        function option_data($matches) {
        	global $options;
        	$arr_data = split("_",$matches[1]);
        	foreach ($arr_data as $key => $key_data) {
        		$eval_data .='["'.strtolower($key_data).'"]';
        	}
        	eval('$return_value = $options'.$eval_data.';');
        	if ($return_value) { 
        		return $return_value; 
        	} else { 
        		return $matches[0]; 
        	}
        }

        preg_replace_callback('/{(.*)}/Ui',"option_data",$question["design"]);

          Write a Reply...