Change the line between two specific tags - php

The updated question, unfortunately, I have not explained this before.

Details:

I have a database with several rows, and each row has tons of data, but each one has tags <pre><code></code></pre>in random order.

example:

$string = "
    <h2>Heading 2</h2>
    Something 
        <pre><code> alot of html and other code here  </code></pre> 
    something something 
        <pre><code> alot of html and other code here  </code></pre>
";


NOTE: the line will not be in the same order each time, there may be more or less tags <pre><code>

now I want to display the code inside the tags <pre><code></code><pre>as plain text, and you know that I need to convert everything "<"with "&lt;"and ">"with "&gt;", but the problem is that there are other tags outside the preliminary tags. if I use htmlspecialchars($string);, it will show all tags as plain text, even h2 tag and pre tags.

, , htmlspecialchars() .

+4
2

, , , :

$str = preg_replace("/<pre><code>[^<]*<\/code><\/pre>/", "<pre><code>other code</code></pre>", $str);

echo $str;

: https://eval.in/669811

Update:

, .

$parts = preg_split("/<pre><code>|<\/code><\/pre>/", $str);

$str = "";
for ($i=0;$i < count($parts);$i++) {
     if ($i%2 == 0) {
         $str .= ($i != 0?"</code></pre>":"").strip_tags($parts[$i]);
     } else {
          $str .= "<pre><code>".htmlentities($parts[$i]);
     }
}

print_r($str);

, , <pre><code> </code></pre>, , . , , HTML , - .

, code, , , code, htmlentities, .

: https://eval.in/671005

0

1. html .
2.split .
3. html .
4. html- php.
5. http://www.smarty.net/ , main.php(code) main.tpl(html-).
6. html , ,

<p>{$textfrom_mysql}</p>
<pre><code>{$codefrom_mysql}</code></pre>
0

Source: https://habr.com/ru/post/1659524/


All Articles