Ignore pre tags when using nl2br ()

I want to ignore the following tags when using nl2br. I have standard pre as well as some special pre tags that are styled. When I do this, these are <br> tags inside the <pre> tags.

 string = "Here is some text <pre> <xml> <item></item> <name></name> </xml> </pre> That includes new lines and carriage returns what can i do to add <p> and <br> tags but not to the text below inside the <pre> tags </pre>. <pre class="brush: csharp; title: ;" title=""> public MainPage() { // select the culture (de, it, fr, tr are supported) var ci = new System.Globalization.CultureInfo("tr-TR"); // de-DE etc // this controls the UI strings System.Threading.Thread.CurrentThread.CurrentUICulture = ci; // this controls number and date formatting (optional in this sample) //System.Threading.Thread.CurrentThread.CurrentCulture = ci; // initialize the component<br> InitializeComponent(); } </pre>"; 

to return it

  echo nl2br($string); Here is some text <br /><br /> <pre> <xml> <item></item> <name></name> </xml> </pre> <br /><br /> That includes new lines and carriage returns<br /> what can i do to add <p> and <br> tags but not to the text below inside<br /> the <pre> tags </pre>.<br /> <br /> <pre class="brush: csharp; title: ;" title=""> <br> public MainPage() { // select the culture (de, it, fr, tr are supported) var ci = new System.Globalization.CultureInfo("tr-TR"); // de-DE etc // this controls the UI strings System.Threading.Thread.CurrentThread.CurrentUICulture = ci; // this controls number and date formatting (optional in this sample) //System.Threading.Thread.CurrentThread.CurrentCulture = ci; // initialize the component<br> InitializeComponent(); } </pre> 

Two tags seem to be ignored <br><br> . I think this may be due to comments.

Use this code.

 function my_nl2br($string){ $string = str_replace("\n", "<br />", $string); if(preg_match_all('/\<pre class="brush: csharp; title: ;"\>(.*?)\<\/pre\>/', $string, $match)){ foreach($match as $a){ foreach($a as $b){ $string = str_replace('<pre class="brush: csharp; title: ;">'.$b.'</pre>','<pre class="brush: csharp; title: ;">'.str_replace("<br />", "\n", $b)."</pre>", $string); } } } return $string; } echo my_nl2br($description); 

But it ignores the carriage return in the <pre class="brush: csharp; title: ;" title=""> <pre class="brush: csharp; title: ;" title=""> .

conclusion of this

 public MainPage() { // select the culture (de, it, fr, tr are supported) var ci = new System.Globalization.CultureInfo("tr-TR"); // de-DE etc // this controls the UI strings System.Threading.Thread.CurrentThread.CurrentUICulture = ci; // this controls number and date formatting (optional in this sample) //System.Threading.Thread.CurrentThread.CurrentCulture = ci; // initialize the component InitializeComponent(); } 
+1
source share
2 answers

Solution from PHP manual comments:

 function my_nl2br($string){ $string = str_replace("\n", "<br />", $string); if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){ foreach($match as $a){ foreach($a as $b){ $string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>", $string); } } } return $string; } 

http://www.php.net/manual/en/function.nl2br.php#100120

+4
source

It's old, but maybe someone else is studying it, so ...

In your example, you use

 <pre class="brush: csharp; title: ;" title=""> 

but in your code you match

 <pre class="brush: csharp; title: ;"> 
0
source

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


All Articles