Hi all,

I am trying to break a large string into substrings based on a maximum length. I have managed to do this on to two lines of 100 characters by using the following code:

if (strlen($itmaDescription[$t] < 200)) {
$str1 = substr($itmaDescription[$t], 0, 100);
$str2 = substr($itmaDescription[$t], 101, 200);

This works ok for two lines but it cuts off the sentence mid word. What I would like to happen is for the substr to go back to the last space below the 100 character position.

I am getting the string from a mySql database and I have tried using the SUBSTRING_INDEX query but I am finding it very difficult to achieve the desired result. I would prefer to break the string within PHP because it fits better with the rest of my code.

I am searching for this info as a workaround because I am outputting to PDFlib and can't get multi line text output working. I am attempting to break the string up then output it to PDFlib. If anyone can help with the bigger PDflib problem then I would be well happy.

Thanks in advance.

T12

    Someone probably has a better work around for you, but this should do the trick, though it is a bit intesive.

    Find the string that is too long.
    Explode the strong on the space (www.php.net/explode)
    Then loop through the returned explode array and add up the string length, and as soon as you get close enough to your desire length, insert the line break. The pseudo code is something like:

    if(string too long){
    explodeArray = explode(theString on " ");
    }
    $i=0;
    while(stringlength < desiredLength){
    echo explodeArray[$i]." ";
    $i++;
    $stringLength += (strlen(explodeArray[$i]) +1) //+1 for the space
    }
    echo line break
    now do the above loop again, but do not reset your $i counter. This will pick up right where you left off in the array.

    The above pseudo code is just to give you an idea, you can easily nest everything into a much prettier package.

      you may also want to look at [man]wordwrap[/man]

        10 days later

        Thanks guys for your help. I've been away for 2 weeks so I am only looking at this now.

        dp777, thanks very much for your idea, it seems the right way for me to go. I have tried to implement your idea and I am sort of getting there but my loop seems to go on for too long. I have adapted your idea to the code below:

        <?php
        
        $sent = "On the first day of Christmas my true love gave to me, a partridge in a pear tree. On the second day of Christmas my true love gave to me, two turtle doves and a partridge in a pear tree. On the third day of Christmas my true love gave to me, three french hens, two turtle doves and a partridge in a pear tree. On the fourth day of Christmas my true love gave to me, four calling birds, three french hens, two turtle doves and a partridge in a pear tree.";
        
        $leng = 50;
        $stringlength = strlen($sent);
        
        if($stringlength > $leng){
           		$explodeArray = explode(" ", $sent);
        }
        
        $i=0;
        $stringfloat = 0;
        $tot = 0;
        while($tot < $stringlength){
        
        	$stringfloat = 0;
        	while($stringfloat < $leng){
        	   echo $explodeArray[$i]." ";
        	   $i++;
        	   $stringfloat = $stringfloat + (strlen($explodeArray[$i]) +1);
        	   $totfloat = $stringfloat;
        	}
        	$tot = $tot + $totfloat;
        
        	echo "<br />";
        }
        ?>

        Below is the output it gives back, as you can see it seems to loop too far. Can anybody see where my code is incorrect?

        On the first day of Christmas my true love gave to
        me, a partridge in a pear tree. On the second day of
        Christmas my true love gave to me, two turtle doves and a
        partridge in a pear tree. On the third day of Christmas my
        true love gave to me, three french hens, two turtle
        doves and a partridge in a pear tree. On the fourth
        day of Christmas my true love gave to me, four
        calling birds, three french hens, two turtle doves and a
        partridge in a pear tree.
        Notice: Undefined offset: 93 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 21

        Notice: Undefined offset: 93 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 19

        Notice: Undefined offset: 94 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 21

        Notice: Undefined offset: 94 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 19

        Notice: Undefined offset: 95 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 21

        Notice: Undefined offset: 95 in /home/default/itanswers.biz/user/htdocs/earth/string.php on line 19

        The undefined offset runs right up to 126 but I can't understand why my loop goes on for so long. It looks like it does its not actually doing its job properly on the breaking of the lines. Some lines in the paragraph above look like they could have more words on and don't reach the limit of 50.

        To reinterate, I am trying to break up a large string so that I can eventually get each line into a new string. The first stage I'm trying get past is seperating the string out onto seperate lines based on a character width of 50 for each line.

        Any help would be much appreciated. Thank you.

        T12

          The first stage I'm trying get past is seperating the string out onto seperate lines based on a character width of 50 for each line.

          That's precisely a job for [man]wordwrap/man, which devinemke just mentioned.

            Thanks for the reply laserlight. I might be wrong here because I am not a PHP expert but what I'm trying to do once I get the above script working is grab each sentence within the loop and output it to an array, so I end up with say 5 substrings (each line) populating an array and Wordwrap() won't allow me to do this.

            The overall purpose for this code is for input into a PDFlib script and we need each line as a seperate variable or array place.

            My understanding of Wordwrap() is that it will do the dropping down onto new lines and not chop up the words so acheiving my output paragraph above but I can't see a way of splitting it up the way we need it.

            Again, I could be wrong.

            Overall what I want to do is input a large string, then break it into seperate lines and populate an array with each line while not chopping words in middle.

            If you or anyone has any ideas for a better approach then please let me know.

            Thanks in advance.

            T12

              Overall what I want to do is input a large string, then break it into seperate lines and populate an array with each line while not chopping words in middle.

              If there's a guarantee that the input string does not contain any new line sequences, then one can do:

              $words = explode('\n', wordwrap($input_str, $max_length));

              If there are input lines, then you may have to decide on some character (sequence) that will never be present in the string, and then break with that character (sequence).

                That looks brilliant, I'd have never thought of doing it like that. It makes a mockery of my attempt LOL.

                I have to go out now but I will try that on my return this afternoon and I will post the results.

                Thanks very much laserlight, much appreciated.

                T12

                  tsk tsk, sorry about that. Switching between C++ and PHP forums can lead to typos.

                  '\n' is a string of 2 characters, the backslash and n.

                  What you need to use (if you're breaking with a newline) is "\n"

                    Thanks LaserLight, that code works a treat and has cute about 15 lines down to one. Many thanks.

                    I have tested it on a test page, now I'm going to try and fit it in with the PDFlib script.

                    Thanks again.

                    T12

                      Write a Reply...