Use of substr()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hello all;
I am scratching my head over the following:
I have written code that is supposed to format text
sent from a form in a textarea form element.
This text does not have new lines added if
the text is allowed to automatically wrap to the
next line. I want to automatically add new lines
to this input at appropriate locations.

Here is the code I wrote:
(It is in early stages of development)
function format_text($a, $b) // $a is text to format, from textarea, $b is line length
                    { $line = array();
                       $limit = (strlen($a)/$b);
                      for($i = 0; $i < $limit; $i++)
                         {
                            $start = $i*$b;
                            $stop = (($i*$b) + $b) - 1;
                            print "start : $start     stop : $stop<br>";
array_push($line, substr($a, $start, $stop)); // separate into lines not greater than $b length
                         }
                    return $line;
                     }

Here is what is entered in a test run:
(actual text has no new lines, I added them here)
Then let us test a long string that spans more than the line length of the text area field. This is the way I practice both writing and typing. If I could only get by the booby traps
and land mines with my typing, I could get somewhere.

This is output code:

if($_POST['input'])
  {print strlen($_POST['input']);
   print'<br>';
   $output = array();
   $output = format_text($_POST['input'], 60);
   for($i = 0; $i < count($output); $i++)
      { print "$i :".$output[$i].'<br>'; }
  }

Here is what comes out the other end:

239
start : 0 stop : 59
start : 60 stop : 119
start : 120 stop : 179
start : 180 stop : 239
0 :Then let us test a long string that spans more than the lin
1 : length of the text area field. This is the way I practice both writing and typing. If I could only get by the booby tr 2 :oth writing and typing. If I could only get by the booby traps and land mines with my typing, I could get somewhere.
3 :ps and land mines with my typing, I could get somewhere.

// e is missing from end of line 0
// portions of the text are repeated out of sequence.
The questions I have are:
What does the browser actually do when it automatically wraps textarea input?
What could explain the scrambled result?

Thanks much to anyone taking the time:
Jeff K

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux