Jump to content

Adding Strings with a while loop


zfred09

Recommended Posts

Alright what I am trying to do is take that string of text at the top and break it up into separate lines instead of one long string(ex 1) and store it like that in a php variable(see ex 2). \n after each 20 char section does not work and I don't merely want <br> after each line because this will only separate them in html.  I actually need the 20 char sections stored like ex 2 in a php variable.  I don't know if I am going about this entirely wrong or not, so suggestions & help would be nice.  The ultimate goal of this script is to break a paragraph of text up into separate lines and then output these with gd library to an image as I couldn't find a way to break lines in gd. 

 

wrong ex 1) If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).

 

ex 2)If length is given and is positive,

      the string returned will contain a

      t most length characters beginni

      ng from start (depending on the

      length of string).

 

<?php 

$text = "If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string).";

$newtext = wordwrap($text, 20, "**");


$arr = explode('**', $newtext);

$arrcount = count($arr);

$count = 0;

while($count <= $arrcount){

$blah = "$arr[$count]";
       
$count++;

}

        	
?>

Link to comment
https://forums.phpfreaks.com/topic/41723-adding-strings-with-a-while-loop/
Share on other sites

Guest footballkid4

$inputString = "Some really long string that has a lot of characters to wrap around multiple lines";
$outputString = "";
$currentIndex = 0;
$inputLength = strlen($inputString);
while ($currentIndex + 20 < $inputLength) {
     $outputString .= substr($inputString, $currentIndex, 20) . "\n";
     $currentIndex += 20;
}
$outputString .= substr($inputString, $currentIndex);

 

There are many other ways to go about this...but this doesn't rely on wordwrap or arrays or anything.  Basically, the input string is what you want to convert, the output string is the separated string.  currentIndex is to tell where in the string the script is currently add.  Each iteration of the while loop adds the next 20 characters, then the final code adds however many characters may be left.

I appreciate your help, but this did not solve the problem.  When I input outputString into a set of gd commands, the gd library does not read \n properly and thus still displays the image of outputString as one long line of text on the image.  I'm looking for a way to have line breaks in the image that the gd library outputs or a way to have line breaks in a string of text before it is compiled/run.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.