Last active
December 19, 2015 17:29
-
-
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…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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