1

我想在使用 nl2br 时忽略以下标签。我有标准的 pre,还有一些特定的 pre 标签。当我这样做时,会将标签贴<br>在标签内<pre>

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>";

退回这个

    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>

似乎忽略了有两个<br><br>标签。我认为这可能与评论有关。

使用此代码。

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);

但它忽略了该部分中的回车<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
    InitializeComponent();
}
4

3 回答 3

5

PHP手册注释的解决方案:

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

于 2011-11-21T19:36:35.300 回答
0

这是旧的,但也许其他人仍在研究这个,所以......

在您的示例中,您使用

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

但是在您匹配的代码中

<pre class="brush: csharp; title: ;">
于 2012-07-23T21:24:24.770 回答
0

只需将此代码段添加到您的页面中

  $(document).ready(function(){
      var str_arr = document.getElementsByTagName("pre");
      for(var i = 0; i < str_arr.length; i++){
          str_arr[i].innerHTML = str_arr[i].innerHTML.replace(/<br>/g, "");
      }
  });

<br><pre>

于 2018-06-28T17:57:15.523 回答