Skip to content

Instantly share code, notes, and snippets.

@BenjaminAdams
Last active December 19, 2015 17:29
Show Gist options
  • Save BenjaminAdams/5992008 to your computer and use it in GitHub Desktop.
Save BenjaminAdams/5992008 to your computer and use it in GitHub Desktop.
Splitting a string between multiple lines. When you are splitting a string to go on multiple lines we want to split the string where it does not break a word onto different lines. This to to make the string easier to read. In this example I was generating a PDF and needed to split a title of something onto 3 different lines so the text did not s…
public function split_title($title,$maxStr)
{
$newStr = Array();
$maxLines=3;
$lineNum = 1; //to be line 1,2, or 3
$split = explode ( " ", $title);
foreach($split as $str)
{
if(strlen($newStr[$lineNum] ." " . $str) > $maxStr && $lineNum !=$maxLines)
{
$lineNum++;
}
$newStr[$lineNum] .= " " . $str;
}
return $newStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment