Generating new lines from string

Associate
Joined
27 Apr 2004
Posts
2,377
I'm using PHP as a sig generator. The sig I'm trying to make uses a random selection of quotes from a text file and displays them on a background graphic. However, the problem is that pretty much all of the quotes are too long to display as a single line, so I'm trying to figure out how I can get PHP to split a single string (randomly selected from the quotes file) into several lines of different length. I've got this far:
PHP:
$quotes = file('quotes.txt');
$quote = $quotes[array_rand($quotes)];

$pieces = explode(" ", $quote);
foreach($pieces as $piece){
echo "$piece<br>";
};
- the last bit was just me testing it worked. I'm a bit of a newbie at PHP having only learnt it from random tutorials etc. What I'm trying to do, is make the script go through the array $pieces, adding words so long as the total string length is less than a certain value - when it reaches this value, or in a case where adding the next word would make in exceed the value, it should start a new line (as a new entry in the output array).

In other words, a script that takes

$pieces
eg. "This is a random quote to show in a sig but it's too long to fit as one line"

as an input, and convert it to this:

$output[] = "This is a random";
$output[] = "quote to show in";
$output[] = "a sig but it's too";
etc.

The length of each output part is specified at some point, so that each part is no longer than that.

Hopefully this makes sense but please ask if I've been unclear. So, with the explanation done - can anyone help me with this please? :)
 
Associate
OP
Joined
27 Apr 2004
Posts
2,377
Thanks for that, I didn't expect the answer to be so simple :D Got it all working now (it wasn't for my sig so that's why there's no change there... yet - I'm going to use it in mine too now I know how).

Thanks again :)
 
Back
Top Bottom