Regular Expression Help!

Discussion in 'PHP' started by cumminsj, Oct 22, 2007.

  1. #1
    Hi All

    Im trying to write a regular expression that takes the HTML of a page in a string
    and then appends my string just before the </body> tag .

    This is what i have to append my string after the </body> but i want my string to go before the </body> tag.

    
    $mystring = '<b>test</b>';
    
    $_html_body = preg_replace('#\<\s*/body(.*?)\>#si', "$0\n$_mystring" , $_html_body, 1);
    
    PHP:
    Thanks in advance
     
    cumminsj, Oct 22, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    $_html_body = preg_replace('~</body>~i', "{$mystring}\n{$0}", $_html_body, 1);
    
    PHP:
    Untested but should work.
     
    nico_swd, Oct 22, 2007 IP