Regular Exp pattern

Discussion in 'PHP' started by snowLee, Jun 11, 2009.

  1. #1
    Can anyone, please help me with a reg exp for this:

    <pre style="color: red">
    <?php
    phpinfo();
    ?>
    </pre>

    I want to look for the first pre tag and for the second one, take only what is between those and to replace all < > tags with &gt; and &lt; .

    Thank you.
     
    snowLee, Jun 11, 2009 IP
  2. neegeris

    neegeris Banned

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    If your page is php, than use single quotes to show other php code
     
    neegeris, Jun 11, 2009 IP
  3. stOK

    stOK Active Member

    Messages:
    114
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    Something like that will transform $text into what you desire.

    
    preg_match('@(<pre(?:>|\s[^>]*?>))(.*?)(</pre>)@ims',$text,$res);
    $replace=array('&gt;','&lt;');
    $search=array('|>|','|<|');
    $newtext=preg_replace($search,$replace,$res[2]);
    
    PHP:
    or
    
    preg_match('@(<pre(?:>|\s[^>]*?>))(.*?)(</pre>)@ims',$text,$res);
    $newtext=htmlentities($res[2]);
    
    PHP:
     
    stOK, Jun 11, 2009 IP
  4. snowLee

    snowLee Member

    Messages:
    55
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #4
    Thank you, but if I have some text before <pre> and after </pre> tags, what will be the reg exp?
     
    snowLee, Jun 11, 2009 IP