php
  • html
  • plugins
  • 2016-07-07 15 views 0 likes 
    0

    Ich habe ein PHP-Plugin, das eine Zeile in mehrere Zeilen explodiert.PHP explode function find last kind

    $graph_lines = explode(";", $content); 
    $output= ''; 
    $output .= '<ul class="lpd-bullet-list'; 
    if($image){ 
        $output .= ' lpd-bl-custom-icon';  
    } 
    if($style){ 
        $output .= ' '.esc_attr($style);  
    } 
    $output .= '">'; 
    
    foreach ($graph_lines as $line) { 
        if($line){ 
    
         $output .= '<li>'; 
         $output .= $image; 
         $output .= $line; 
         $output .= ';'; 
         $output .= '</li>'; 
    
        } 
    } 
    
    $output .= '</ul>'; 
    echo $output; 
    

    Nach jeder Zeile am Ende gesetzt es ; Das ist in Ordnung, aber wie . für letztes Element zu setzen? Ist es möglich?

    Mey muss ich end() Funktion oder eine andere verwenden?

    Hilf mir bitte!

    +0

    können Sie die erwartete Ausgabe posten? –

    Antwort

    1

    Sie können die Anzahl der Linien sizeof($graph_lines) bekommen und einen Zähler in Ihrem foreach erstellen.

    Exemple:

    $numberofline = sizeof($graph_lines); 
        $i=0; 
        foreach ($graph_lines as $line) { 
         if($line){ 
          $i++; 
          $endofline=";"; 
          if($i==$numberofline) 
          { 
           $endofline="."; 
          } 
          $output .= '<li>'; 
          $output .= $image; 
          $output .= $line; 
          $output .= $endofline; 
          $output .= '</li>'; 
    
         } 
        } 
    
    +0

    Fehler erhalten, etwas stimmt nicht –

    +0

    Überprüfen Sie meine aktualisierte Antwort – sxmboy

    +0

    Das ist in Ordnung! Vielen Dank! –

    2

    Ich bin nicht ganz sicher über die Ausgabe aber ja können Sie Ende Funktion wie folgt verwendet werden:

    $last_index = end(array_keys($graph_lines)); 
    foreach ($graph_lines as $index => $line) { 
        if ($index == $last_index) { 
         // last index 
        } else { 
         // perform other tasks 
        } 
    } 
    

    Alternativ können Sie wie folgt verwenden können:

    foreach ($graph_lines as $line) { 
        if($line){ 
    
         $output .= '<li>'; 
         $output .= $image; 
         $output .= $line; 
         $output .= ';'; 
         if (next($graph_lines)==false) $output .= '.';//not sure where you put it. 
         $output .= '</li>'; 
    
        } 
    } 
    
    1

    das Array Count. und überprüfen Sie das mit der $i.

    $count = count($graph_lines); 
    $i = 0; 
    foreach ($graph_lines as $line) { 
        if ($line) { 
         if ($i == $count) { 
          $output .= '<li>'; 
          $output .= $image; 
          $output .= $line; 
          $output .= '.'; 
          $output .= '</li>'; 
         } else { 
          $output .= '<li>'; 
          $output .= $image; 
          $output .= $line; 
          $output .= ';'; 
          $output .= '</li>'; 
         } 
        } 
        $i++; 
    } 
    
    +0

    // Ausgabe - was ist das? –

    +0

    '// output' ist das, was Sie zuletzt ausgeben möchten. Zeigen Sie uns, welche Ausgabe Sie wollen – urfusion

    +0

    Ich möchte Punkt ausgeben, also muss ich $ output. = '.'; –

    1
    $content = "a;b;c;d;s;r;t;g"; 
    $content = rtrim($content, ";"); 
    $content = ltrim($content, ";"); 
    $graph_lines = explode(";", $content); 
    $output = ''; 
    $output .= '<ul class="lpd-bullet-list">'; 
    
    $last = count($graph_lines) - 1; 
    $i = 0; 
    foreach ($graph_lines as $line) { 
        if ($line) { 
         $output .= '<li>'; 
         $output .= $line; 
         $output .= ($last == $i)? '.' : ";"; 
         $output .= '</li>'; 
         $i++; 
        } 
    } 
    
    $output .= '</ul>'; 
    echo $output; 
    
    1

    Sie können eine Kurzschreibweise verwenden, wenn prüfen, ob die aktuelle Zeile wird in dem Feld das letzte Element entspricht.

    foreach ($graph_lines as $line) { 
        if($line){ 
    
         $output .= '<li>'; 
         $output .= $image; 
         $output .= $line; 
         $output .= ($line === end ($graph_lines)) ? '.' : ';'; 
         $output .= '</li>'; 
    
        } 
    } 
    

    Ein anderer Weg ist, einen Zähler zu setzen und prüfen, ob der Zähler auf die count() von $ graph_lines gleich ist.

    $counter = 1; 
    $total_items = count ($graph_lines); 
    
    foreach ($graph_lines as $line) { 
        // do your stuff. 
        $output .= ($counter === $total_items) ? '.' : ';'; 
        $counter++; 
    } 
    
    Verwandte Themen