newbtophp Posted June 5, 2010 Share Posted June 5, 2010 Hi, Im working on alittle NFO outputer, but the trouble is long text and indentation is not done accurately (it goes out of place). I want to add indentation (3 spaces before the start and end of a line) and any long text to be broken and proceeded to the next line (\n) (if neccesary) - my code: (refer to the expected output to see how im trying to get it too look like). <?php $nfo_text = <<<eof ################################################################### # default text sample of how it should be # # {text} # # ################################################################### eof; function wrap($text) { $start_indent = " "; $end_indent = " "; $start = "#".$start_indent; $end = $end_indent."#"; return $start.$text.$end; } $text = "phpfreaks ftw, somelongtexttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt hey how are you?"; $nfo_text = str_replace('{text}', wrap($text), $nfo_text); echo "<pre>"; echo($nfo_text); echo "</pre>"; ?> Output: ################################################################### # default text sample of how it should be # # # phpfreaks ftw, somelongtexttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt hey how are you?# # # ################################################################### Expected Output: ################################################################### # default text sample of how it should be # # # phpfreaks ftw, somelongtexttttttttttttttttttttttttttttttttttt # # tttttttttttttttttttttttttt hey how are you? # # # ################################################################### Anyone can help me? :-\ Quote Link to comment Share on other sites More sharing options...
ignace Posted June 5, 2010 Share Posted June 5, 2010 $maximumLength = 70; echo str_repeat('#', $maximumLength + , "\n", '# ', wordwrap($text, $maximumLength, " #\n# ", true), " #\n", str_repeat('#', $maximumLength + ; Quote Link to comment Share on other sites More sharing options...
newbtophp Posted June 5, 2010 Author Share Posted June 5, 2010 Hmm ignace I integrated your code: <?php $nfo_text = <<<eof ################################################################### # default text sample of how it should be # # {text} # # ################################################################### eof; $maximumLength = 70; function wrap($text) { global $maximumLength; return str_repeat('#', $maximumLength + . "\n". '# '.wordwrap($text, $maximumLength, " #\n# ", true). " #\n". str_repeat('#', $maximumLength + ; } $text = "phpfreaks ftw, somelongtexttttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt hey how are you?"; $nfo_text = str_replace('{text}', wrap($text), $nfo_text); echo "<pre>"; echo($nfo_text); echo "</pre>"; ?> But the output is: ################################################################### # default text sample of how it should be # # ############################################################################## # phpfreaks ftw, # # somelongtexttttttttttttttttttttttttttttttttttttttttttttttttttttttttttt # # tt hey how are you? # ############################################################################## # # ################################################################### Quote Link to comment Share on other sites More sharing options...
newbtophp Posted June 5, 2010 Author Share Posted June 5, 2010 Anyone can help me? plz. xD Quote Link to comment Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 function wrapify($text, $maximumLineLength = 70, $callback = '') { $words = explode(' ', $text); $line = ''; $lines = ''; foreach ($words as $word) { if (strlen($line . $word) > $maximumLineLength) { $line = sprintf("% -{$maximumLineLength}s", $line); if (function_exists($callback)) { $line = $callback($line); } $lines .= $line; $line = ''; } $line .= $word . ' '; } if (!empty($line)) { $line = sprintf("% -{$maximumLineLength}s", $line); if (function_exists($callback)) { $line = $callback($line); } $lines .= $line; } return $lines; } function wrapifyCallback($line) { return "# $line #\n"; } $text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' . 'Ut dictum mattis lobortis. Phasellus velit mauris, mattis non accumsan in, ' . 'elementum auctor tortor. Praesent eget dui nec odio egestas ullamcorper. ' . 'Sed gravida bibendum quam. Nullam varius augue a quam iaculis fermentum nec et enim. ' . 'Nullam vel sagittis tortor. Cras a ultrices.'; $maximumLineLength = 70; echo str_repeat('#', $maximumLineLength + , "\n", wrapify($text, $maximumLineLength, 'wrapifyCallback'), str_repeat('#', $maximumLineLength + , "\n"; Outputs: ############################################################################## # Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut dictum # # mattis lobortis. Phasellus velit mauris, mattis non accumsan in, # # elementum auctor tortor. Praesent eget dui nec odio egestas # # ullamcorper. Sed gravida bibendum quam. Nullam varius augue a quam # # iaculis fermentum nec et enim. Nullam vel sagittis tortor. Cras a # # ultrices. # ############################################################################## Quote Link to comment Share on other sites More sharing options...
newbtophp Posted June 6, 2010 Author Share Posted June 6, 2010 Great! works great ignace, however does'nt work with line breaks/new lines: Example Text Scenario: $text = "1. Extract the zip files to a folder on your computer. 2. Follow the instructions in readme.txt"; Would brake out of the # border and not proceed to the next line properly. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 Great! works great ignace, however does'nt work with line breaks/new lines: Example Text Scenario: $text = "1. Extract the zip files to a folder on your computer. 2. Follow the instructions in readme.txt"; Would brake out of the # border and not proceed to the next line properly. Yeah, and what do you expect of me now? That I write this for you, for free? I already did more for you then I had to. Either try it yourself or pay me. Quote Link to comment Share on other sites More sharing options...
newbtophp Posted June 6, 2010 Author Share Posted June 6, 2010 Great! works great ignace, however does'nt work with line breaks/new lines: Example Text Scenario: $text = "1. Extract the zip files to a folder on your computer. 2. Follow the instructions in readme.txt"; Would brake out of the # border and not proceed to the next line properly. Yeah, and what do you expect of me now? That I write this for you, for free? I already did more for you then I had to. Either try it yourself or pay me. lol sorry. I tried myself - always do before posting here, ill retry - but pointers would help. Quote Link to comment Share on other sites More sharing options...
ignace Posted June 6, 2010 Share Posted June 6, 2010 You should modify the code so that it detects newlines and executes: $line = sprintf("% -{$maximumLineLength}s", $line); if (function_exists($callback)) { $line = $callback($line); } $lines .= $line; Quote Link to comment Share on other sites More sharing options...
newbtophp Posted June 6, 2010 Author Share Posted June 6, 2010 Hmm I've tried the following but no luck: function wrapify($text, $maximumLineLength = 70, $callback = '') { $lines = explode("\n", $text); $words = null; foreach($lines as $line) { $words .= implode(", ", explode(' ', $line)); } $words = explode(", ", $words); $line = ''; $lines = ''; foreach ($words as $word) { if (strlen($line . $word) > $maximumLineLength) { $line = sprintf("% -{$maximumLineLength}s", $line); if (function_exists($callback)) { $line = $callback($line); } $lines .= $line; $line = ''; } $line .= $word . ' '; } if (!empty($line)) { $line = sprintf("% -{$maximumLineLength}s", $line); if (function_exists($callback)) { $line = $callback($line); } $lines .= $line; } return $lines; } Quote Link to comment Share on other sites More sharing options...
Alex Posted June 7, 2010 Share Posted June 7, 2010 Here's my input: function wrap($text, $maxLineLength = 70){ $lines = explode("\n", wordwrap_cut($text, $maxLineLength)); $out = ''; foreach($lines as $line){ $out .= "# " . str_pad($line, $maxLineLength) . " #\n"; } return str_repeat('#', $maxLineLength + . "\n" . $out . str_repeat('#', $maxLineLength + ; } function wordwrap_cut($text, $maxLineLength, $break = "\n"){ $split = str_split($text); for($i = 0, $n = floor(sizeof($split) / $maxLineLength);$i < $n;++$i){ if(strpos(substr($text, $i * $maxLineLength, ($i + 1) * $maxLineLength), $break) === false){ array_splice($split, ($i + 1) * $maxLineLength, 0, $break); } } return implode('', $split); } $text = "1. Extract the zip files to a folder on your computer.\n2. Follow the instructions in readme.txt"; echo wrap($text); Output: ############################################################################## # 1. Extract the zip files to a folder on your computer. # # 2. Follow the instructions in readme.txt # ############################################################################## Quote Link to comment Share on other sites More sharing options...
newbtophp Posted June 8, 2010 Author Share Posted June 8, 2010 Thanks Alex that worked perfectly Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.