[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

[PHP] wordwrap funktion igen. finde sidste mellemrumpå linje



hejsa 
jeg har en php wordwrap funktion som tager en lang streng og først deler
den op i linjer (ud fra \n) og derefter sørger for at linjerne klippes
op og sættes ind mellem "|  |" så det kan danne en form for "tabeller". 
Mit problem er bare at min regex ikke finder rigtigt tilbage til sidste
mellemrum (hvor den skal dele linjen), men i stedet deler i midten af
ord (se på www.linuxia.dk hvor scriptet bruges).

Koden til funktionen ser således ud(linjen markeret med ---> er den som
problemet er i):

function word_wrap2($wrap, $text){
    $wrap -=3;
    $count = 0;
#   $text = preg_replace("/\n/","", $text);
    foreach (split("\n",$text) as $line){
#       $line = preg_replace("/$0/","",$line);
        if (strlen($line) <= $wrap){
            print "| " .$line;
            for($x=0;$x<($wrap-strlen($line)); $x++){
                 print " ";
            }
            print " |\n";
        }else{
            $substring = " ";
            while(strlen($line)-$count >0){
               $substring = substr($line,$count, $wrap);
               #       print $substring;
---->          if(preg_match("/^(.*?)\s([^\s]+)$/",$substring ,$match)){
                   $count = $count+$wrap;
                   $substring = $match[0];
               } else $count = strlen($substring);
               print "| ";
               print $substring;
               for ($x=0;$x<($wrap-strlen($substring)); $x++){
                   print " ";
               }
               print " |\n";
        }
        $count=0;
   }
}




}

-- 
Kim Schulz - Freelance Development    | A bird in the hand makes it
www.schulz.dk  - En nørds bekendelser | awfully hard to blow your
www.linuxia.dk - hverdagens små hacks | nose. 


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2005-08-10, 19:58 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *