dpdhnd3577 2011-11-21 19:32
浏览 54

使用nl2br()时忽略pre标签

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("
", "<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 />", "
", $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();
}
  • 写回答

3条回答 默认 最新

  • duanmu3049 2011-11-21 19:36
    关注

    Solution from PHP manual comments:

    function my_nl2br($string){
    $string = str_replace("
    ", "<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

    评论

报告相同问题?

悬赏问题

  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题
  • ¥15 java报错:使用mybatis plus查询一个只返回一条数据的sql,却报错返回了1000多条
  • ¥15 Python报错怎么解决
  • ¥15 simulink如何调用DLL文件
  • ¥15 关于用pyqt6的项目开发该怎么把前段后端和业务层分离
  • ¥30 线性代数的问题,我真的忘了线代的知识了
  • ¥15 有谁能够把华为matebook e 高通骁龙850刷成安卓系统,或者安装安卓系统