Regular Expression Help: Passing Variables to Functions in Replacement Strings

Discussion in 'PHP' started by Nathan Malone, Feb 17, 2006.

  1. #1
    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!
     
    Nathan Malone, Feb 17, 2006 IP
  2. eKstreme

    eKstreme Guest

    Messages:
    131
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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...
     
    eKstreme, Feb 18, 2006 IP
  3. Nathan Malone

    Nathan Malone Well-Known Member

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #3
    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!
     
    Nathan Malone, Feb 18, 2006 IP
  4. Nathan Malone

    Nathan Malone Well-Known Member

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #4
    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!
     
    Nathan Malone, Feb 18, 2006 IP
  5. Nathan Malone

    Nathan Malone Well-Known Member

    Messages:
    369
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    110
    #5
    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):
     
    Nathan Malone, Feb 18, 2006 IP
  6. eKstreme

    eKstreme Guest

    Messages:
    131
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Glad it's working. I doubt you'll forget this solution forever :)
     
    eKstreme, Feb 20, 2006 IP