Array Problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Guest's Avatar

    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.


  • jn

    #2
    Re: Array Problem


    "<>" <loopback@44.25 5.255.255> wrote in message
    news:9H9rb.1247 59$ZH4.91655@tw ister.socal.rr. com...[color=blue]
    > I thought I understood how to traverse an array, but I guess I was wrong.[/color]
    I[color=blue]
    > have tried writing the code snippet below as: while, for, foreach... and[/color]
    get[color=blue]
    > the same (consistently wrong) result each time!
    >
    > $data is a string variable containing lines of text. Each line is[/color]
    terminated[color=blue]
    > with a break (<br>). I need to analyze each line to ensure it does not[/color]
    exceed[color=blue]
    > $maxLength. If it is <= $maxLength do nothing, otherwise truncate the line[/color]
    at[color=blue]
    > $maxLength minus 3 and add ellipses (...). I then need to reassemble
    > everything back into a string variable, for later printing. Here is my[/color]
    code:[color=blue]
    >
    > $line = explode('<br>', $data);
    > $count = count($line);
    > $i=1;
    > while ($i <= $count) {
    > $line = (strlen($line)> $maxLength) ?[/color]
    (substr($line,0 ,($maxLength-3)).'...')[color=blue]
    > : $line;
    > $i++;
    > }
    > $data = implode('<br>', $line);
    > echo $data;
    >
    > I know I am forming the array correctly, because $line and $count display[/color]
    as[color=blue]
    > expected. I know that the ternary is correct, because it produces the[/color]
    desired[color=blue]
    > result using the original string (i.e., substituting $data outside the
    > 'while' loop).
    >
    > Any help will be greatly appreciated.
    >[/color]

    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


    Comment

    • Pedro Graca

      #3
      Re: Array Problem

      [ not posted to alt.php ]

      <> wrote:[color=blue]
      > $line = explode('<br>', $data);[/color]
      ### $line is an array[color=blue]
      > $count = count($line);
      > $i=1;
      > while ($i <= $count) {
      > $line = (strlen($line)> $maxLength) ? (substr($line,0 ,($maxLength-3)).'...') : $line;[/color]
      ### ^^^^^^^^^^^^^ strlen( <array> ) ????[color=blue]
      > $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).[/color]

      try:

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


      Happy Coding :)

      --
      ..sig

      Comment

      • Guest's Avatar

        #4
        Re: Array Problem


        "jn" <jsumner1@cfl.r r.com> wrote in message
        news:jP9rb.3242 1$jW5.547876@tw ister.tampabay. rr.com...[color=blue]
        > "<>" <loopback@44.25 5.255.255> wrote in message
        > news:9H9rb.1247 59$ZH4.91655@tw ister.socal.rr. com...[/color]
        <snip>[color=blue][color=green]
        > > $data is a string variable containing lines of text. Each line is[/color]
        > terminated[color=green]
        > > with a break (<br>). I need to analyze each line to ensure it does not[/color]
        > exceed[color=green]
        > > $maxLength. If it is <= $maxLength do nothing, otherwise truncate the[/color][/color]
        line[color=blue]
        > at[color=green]
        > > $maxLength minus 3 and add ellipses (...). I then need to reassemble
        > > everything back into a string variable, for later printing. Here is my[/color]
        > code:[color=green]
        > >
        > > $line = explode('<br>', $data);
        > > $count = count($line);
        > > $i=1;
        > > while ($i <= $count) {
        > > $line = (strlen($line)> $maxLength) ?[/color]
        > (substr($line,0 ,($maxLength-3)).'...')[color=green]
        > > : $line;
        > > $i++;
        > > }
        > > $data = implode('<br>', $line);
        > > echo $data;
        > >[/color][/color]
        <snip>[color=blue]
        >
        > 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];[/color]

        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?


        Comment

        Working...