ClarkeRubber

String Wrapping

Dec 5th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.86 KB | None | 0 0
  1. <?php
  2. function wrapper($stringText, $width){
  3.     $arrayText = explode("\n", $stringText);
  4.     foreach($arrayText as $temp){
  5.         $tempArray[] = explode(" ", $temp);
  6.     }
  7.  
  8.     var_dump($tempArray);
  9.  
  10.     $output = "";
  11.  
  12.     foreach($tempArray as $wordArray){
  13.         $tempString = "";
  14.         $tempOutput = "";
  15.  
  16.         foreach($wordArray as $word){
  17.             if(strlen($tempString." ".$word) >= $width){
  18.                 if(substr($tempString, -1) != "\n"){
  19.                     $tempString .= "\n";
  20.                 }
  21.                 $tempOutput .= $tempString;
  22.                 $tempString = $word;
  23.             }else{
  24.                 if($tempString == ''){
  25.                     $tempString = $word;
  26.                 }else{
  27.                     $tempString .= " ".$word;  
  28.                 }
  29.             }
  30.         }
  31.  
  32.         $output .= $tempOutput.$tempString;
  33.     }
  34.  
  35.     return $output;
  36. }
  37.  
  38. $string = <<<END
  39. here's some text
  40. I hope you enjoy it there shouldn't be too many problems with it
  41. but if there is don't come winging
  42. END;
  43.  
  44. echo wrapper($string, 20);
  45. ?>
Advertisement
Add Comment
Please, Sign In to add comment