1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Generating new lines from string

Discussion in 'HTML, Graphics & Programming' started by Mr. Brightside, 22 Jan 2006.

  1. Mr. Brightside

    Wise Guy

    Joined: 27 Apr 2004

    Posts: 2,378

    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:
    $quotes file('quotes.txt');
    $quote $quotes[array_rand($quotes)];

    $pieces explode(" "$quote);
    $pieces as $piece){
    - 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

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

    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? :)
  2. Beansprout

    Man of Honour

    Joined: 31 Jan 2004

    Posts: 16,312

    Location: Plymouth

  3. robmiller


    Joined: 26 Dec 2003

    Posts: 16,522

    Location: London

    Using wordwrap() will make sig-making a hell of a lot easier, I promise :)
  4. Mr. Brightside

    Wise Guy

    Joined: 27 Apr 2004

    Posts: 2,378

    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 :)