The following code doesn't work, any pointers? (Note: The code that is probably wrong is that preg_replace on the last line). <?php function parse_html_tags($text, $array) { print $text; print_r($array); return $text; } $key = 'b'; $value = array( 'value1' => 'Value 1', 'value2' => 'Value 2', 'value3' => 'Value 3', ); $text = 'This is <b>very</b> important.'; $text = preg_replace("/<$key>(.*?)<\/$key>/ise", "parse_html_tags('\\1', '$value')", $text); ?> Code (markup): The code above simple prints out "veryArray". Any help is appreciated!
Take out the quotes around the parse_html_tags() call: $text = preg_replace("/<$key>(.*?)<\/$key>/ise", parse_html_tags('\\1', '$value'), $text); Code (markup): Otherwise, it will get treated as a string...
Thank you for the help! That almost fixed it, but now, the '\\1' isn't displaying correctly. I also had to take out the single quotes around the $value to get it to work correctly. Here is the code I used: $text = preg_replace("/<$key>(.*?)<\/$key>/ise", parse_html_tags('\\1', $value), $text); Code (markup): This code prints out the following: It also doesn't work if I take out one of the slashes from before the "1". Do you know how to fix this? Thank you!
To clarify, I basically want to figure out how to make values from the search string appear in the replace string without enclosing the replace string in double quotes, though if there is another way to get it to work, that is also fine. Any help is appreciated!
Okay, I figured out how to do it. For future reference, the code is below. I ended up putting double quotes back around the replacement string, and backslashing the $ in the variable. $text = preg_replace("/<$key>(.*?)<\/$key>/ise", "parse_html_tags('\\1', \$value)", $text); Code (markup):