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:
- 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?
PHP:
$quotes = file('quotes.txt');
$quote = $quotes[array_rand($quotes)];
$pieces = explode(" ", $quote);
foreach($pieces as $piece){
echo "$piece<br>";
};
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?