Array Problem

This is a discussion on Array Problem within the PHP Language forums, part of the PHP Programming Forums category; I thought I understood how to traverse an array, but I guess I was wrong. I have tried writing the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-08-2003
 
Posts: n/a
Default Array Problem

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.


Reply With Quote
  #2 (permalink)  
Old 11-08-2003
jn
 
Posts: n/a
Default Re: Array Problem


"<>" <loopback@44.255.255.255> wrote in message
news:9H9rb.124759$ZH4.91655@twister.socal.rr.com.. .
> 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.
>


You aren't using your counter for anything. You are trying to run this on
every element, not the whole array at once. You should do this:


$line[$i] = (strlen($line[$i])>$maxLength) ?
(substr($line[$i],0,($maxLength-3)).'...') : $line[$i];

HTH


Reply With Quote
  #3 (permalink)  
Old 11-08-2003
Pedro Graca
 
Posts: n/a
Default Re: Array Problem

[ not posted to alt.php ]

<> wrote:
> $line = explode('<br>', $data);

### $line is an array
> $count = count($line);
> $i=1;
> while ($i <= $count) {
> $line = (strlen($line)>$maxLength) ? (substr($line,0,($maxLength-3)).'...') : $line;

### ^^^^^^^^^^^^^ strlen( <array> ) ????
> $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).


try:

<?php
$line = explode('<br>', $data);
foreach ($line as $currline) {
while (strlen($currline) > $maxLength) {
### As I don't want to change the array inside the loop,
### I'm creating another array for the results
$newline[] = (substr($currline, 0, ($maxLength - 3)) . '...');
$currline = substr($currline, ($maxLength - 3));
}
$newline[] = $currline;
}
$newdata = implode('<br/>', $newline);
echo $newdata;
?>


Happy Coding :)

--
..sig
Reply With Quote
  #4 (permalink)  
Old 11-08-2003
 
Posts: n/a
Default Re: Array Problem


"jn" <jsumner1@cfl.rr.com> wrote in message
news:jP9rb.32421$jW5.547876@twister.tampabay.rr.co m...
> "<>" <loopback@44.255.255.255> wrote in message
> news:9H9rb.124759$ZH4.91655@twister.socal.rr.com.. .

<snip>
> > $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;
> >

<snip>
>
> You aren't using your counter for anything. You are trying to run this on
> every element, not the whole array at once. You should do this:
>
>
> $line[$i] = (strlen($line[$i])>$maxLength) ?
> (substr($line[$i],0,($maxLength-3)).'...') : $line[$i];


I could have sworn I already tried that! Oh well... thanks for your help.
I'll learn.

Now that this snippet is working, it has uncovered another problem. That is,
if a <pre> is part of $data, the ternary is ineffective on that substring. I
guess I can solve that with an if/else and substr. Do you think it is better
to do it inside or outside of the loop?


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:54 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0