使用 nl2br() 时忽略 pre 标记

发布于 2024-12-17 10:22:05 字数 3499 浏览 1 评论 0原文

我想在使用 nl2br 时忽略以下标签。我有标准的 pre,还有一些特定样式的 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>

似乎忽略了有两个

标签。我认为这可能与评论有关。

使用此代码。

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

但它忽略

 部分。

输出这个

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

I want to ignore the following tags when using nl2br. I have standard pre, and also some specific pre tags that are styled. When i do this is sticks <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 this

    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>

It seems to ignore that there is two <br><br> tags. Im thinking it might be something to do with the 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 ignore carriage returns in the <pre class="brush: csharp; title: ;" title=""> section.

outputting 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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

国产ˉ祖宗 2024-12-24 10:22:05

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

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

帅哥哥的热头脑 2024-12-24 10:22:05

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

在您的示例中您使用

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

但在您的代码中您匹配

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

This is old, but maybe someone else is still looking into this, so...

In your example you use

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

but in your code you match for

<pre class="brush: csharp; title: ;">
A君 2024-12-24 10:22:05

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

  $(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, "");
      }
  });

即可从

 中删除 

Just add this snippet into your page

  $(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, "");
      }
  });

This removes <br> from <pre>

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文