Array Problem

From: <> (loopback_at_44.255.255.255)
Date: 11/08/03


Date: Sat, 08 Nov 2003 17:26:29 GMT

I thought I understood how to traverse an array, but I guess I was wrong. I
have tried writing the code snippet below as: while, for, foreach... and get
the same (consistently wrong) result each time!

$data is a string variable containing lines of text. Each line is terminated
with a break (<br>). I need to analyze each line to ensure it does not exceed
$maxLength. If it is <= $maxLength do nothing, otherwise truncate the line at
$maxLength minus 3 and add ellipses (...). I then need to reassemble
everything back into a string variable, for later printing. Here is my code:

$line = explode('<br>', $data);
$count = count($line);
$i=1;
while ($i <= $count) {
  $line = (strlen($line)>$maxLength) ? (substr($line,0,($maxLength-3)).'...')
: $line;
  $i++;
}
 $data = implode('<br>', $line);
echo $data;

I know I am forming the array correctly, because $line and $count display as
expected. I know that the ternary is correct, because it produces the desired
result using the original string (i.e., substituting $data outside the
'while' loop).

Any help will be greatly appreciated.



Relevant Pages

  • Array Problem
    ... I thought I understood how to traverse an array, but I guess I was wrong. ... have tried writing the code snippet below as: while, for, foreach... ... If it is <= $maxLength do nothing, ...
    (alt.php)
  • Re: Workbooks collection is empty even though the some of the document is open
    ... used in VB/A to return, say, an array of object ref's to all Excel ... Peter T ... > your code snippet and see if I can help you with it. ...
    (microsoft.public.excel.programming)
  • Re: Array Problem
    ... > I thought I understood how to traverse an array, but I guess I was wrong. ... > have tried writing the code snippet below as: ...
    (comp.lang.php)
  • Re: Reflection and Arrays
    ... unless the array is pre-populated in code that is ... not in the context of this method (the one that makes reflection). ... Code Snippet ... How can I dynamically link this to ...
    (microsoft.public.dotnet.languages.csharp)
  • How can I use constants?
    ... I have an array that I want to reference the different items by a ... constant instead of the actual value (quick code snippet example): ... file the array from a file ... constant values from the actionscript into the perl code and not have ...
    (comp.lang.perl.misc)