2011-11-21 49 views
1

我想在使用nl2br時忽略以下標籤。我有標準的pre,也有一些特定的pre標籤是風格的。當我這樣做的時候,是在<pre>標籤裏面粘上<br>標籤。在使用nl2br時忽略預標籤()

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="">部分中的回車。

輸出從PHP手冊評論這個

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

解決方案:

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

+0

該解決方案沒有工作對我來說。我已更新問題以獲得更多的支持。 – madphp

+0

進行了另一次更新。 – madphp

0

這是舊的,但也許別人還在尋找到這一點,所以...

在你的例子中你使用

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

,但在你的代碼匹配

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