Justifying text means aligning text within a column so that the text is flush with both the left and right margins.
<?php $myText = <<< END_TEXT Hypertext Markup Language (HTML) is the standard markup language for creating web pages and web applications. With Cascading Style Sheets (CSS) and JavaScript, it forms a triad of cornerstone technologies for the World Wide Web.[4] Web browsers receive HTML documents from a web server or from local storage and render the documents into multimedia web pages. HTML describes the structure of a web page semantically and originally included cues for the appearance of the document. HTML elements are the building blocks of HTML pages. With HTML constructs, images and other objects such as interactive forms may be embedded into the rendered page. HTML provides a means to create structured documents by denoting structural semantics for text such as headings, paragraphs, lists, links, quotes and other items. HTML elements are delineated by tags, written using angle brackets. Tags such as <img /> and <input /> directly introduce content into the page. Other tags such as <p> surround and provide information about document text and may include other tags as sub-elements. Browsers do not display the HTML tags, but use them to interpret the content of the page. https://en.wikipedia.org/wiki/HTML END_TEXT; $myText = str_replace( "\r\n", "\n", $myText ); $lineLength = 40; $myTextJustified = ""; $numLines = substr_count( $myText, "\n" ); $startOfLine = 0; for ( $i=0; $i < $numLines; $i++ ) { $originalLineLength = strpos( $myText, "\n", $startOfLine ) - $startOfLine; $justifiedLine = substr( $myText, $startOfLine, $originalLineLength ); $justifiedLineLength = $originalLineLength; while ( $i < $numLines - 1 && $justifiedLineLength < $lineLength ) { for ( $j=0; $j < $justifiedLineLength; $j++ ) { if ( $justifiedLineLength < $lineLength && $justifiedLine[$j] == " " ) { $justifiedLine = substr_replace( $justifiedLine, " ", $j, 0 ); $justifiedLineLength++; $j++; } } } $myTextJustified .= "$justifiedLine\n"; $startOfLine += $originalLineLength + 1; } echo $myText; echo $myTextJustified; ?>