игнорировать предварительные теги при использовании nl2br () - PullRequest
1 голос
/ 21 ноября 2011

Я хочу игнорировать следующие теги при использовании nl2br.У меня есть стандартные pre, а также некоторые конкретные pre-теги, которые оформлены.Когда я это делаю, это вставляет теги <br> внутри тегов <pre>.

<code>string = "Here is some text

<pre>
   <xml>
     <item></item>
     <name></name>
   </xml>
Это включает в себя новые строки и возврат каретки, что я могу сделать, чтобы добавить теги

и
, но не к тексту нижевнутри

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

чтобы вернуть это

<code>    echo nl2br($string);

    Here is some text
    <br /><br />
    <pre>
       <xml>
         <item></item>
         <name></name>
       </xml>
    
Это включает в себя новые строки и возврат кареткиЧто я могу сделать, чтобы добавить

и
теги, но не к тексту ниже внутри

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

Кажется, игнорируется, что есть два тега <br><br>.Я думаю, что это может быть связано с комментариями.

Используйте этот код.

<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.'
','
'.str_replace("
", "\n", $b)."
", $ 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();
}

Ответы [ 3 ]

4 голосов
/ 21 ноября 2011

Решение из комментариев к руководству по PHP:

<code>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.'
', "
".str_replace("<br />", "", $b)."
", $ string);}}} return $ string;}

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

0 голосов
/ 28 июня 2018

Просто добавьте этот фрагмент на свою страницу

  $(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>

0 голосов
/ 24 июля 2012

Это старый, но, может быть, кто-то еще изучает это, так что ...

В вашем примере вы используете

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

, но в вашем коде вы подходите для

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