Jump to content

Help with indentation and text wraping.


newbtophp

Recommended Posts

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?  :-\

Link to comment
Share on other sites

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?   #
##############################################################################
      #                                                                 #
      ###################################################################

Link to comment
Share on other sites

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.                                                                #

##############################################################################

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
}

Link to comment
Share on other sites

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                                 #
##############################################################################

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.