Skip to content

Instantly share code, notes, and snippets.

@esilvas
Created December 12, 2012 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save esilvas/4268767 to your computer and use it in GitHub Desktop.
Save esilvas/4268767 to your computer and use it in GitHub Desktop.
Word wrap with new-lines
public function pdf_word_wrap( $text = '', $line_length = 60 )
{
	    if($text) 
	    {
        $tmp_text = '';
		    $tmp_text_array = array();

		    $total_chars = strlen($text);
		    $text_array = explode(' ', $text);

		    // create array of lines of minimum length
		    foreach($text_array AS $snippet)
		    { 
			    $tmp_text .= $snippet.' ';

			    if (strlen($tmp_text) >= $line_length)
			    {
				    $tmp_text_array[] = trim($tmp_text);
				    $tmp_text = '';
			    }
		    }
		    if (strlen($tmp_text) > 0)
			  {
				  $tmp_text_array[] = trim($tmp_text);
			  }

		  return $tmp_text_array;
	  }
	  else
	  {
		  return FALSE;
	  }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment