HELP! php function - count string

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    HELP! php function - count string

    Could anyone provide me a simple php function to count character of each lines on a string and get the longest one as an output?

    String:
    this is the string
    with newline
    what is the output?

    Output: 19 (line 3)

    #2
    Use maths max() function.
    <!DOCTYPE html PUBLIC "-//WAPFORUM.RS

    Comment


      #3
      Here's an ugly "algorithm":

      PHP Code:
      <?php

      $string 
      "this is the string
       with newline
       what is the output?"
      ;
       
       
      $arr explode(PHP_EOL$string);
       
       
      $max strlen(trim($arr[0]));
       
      $line 1;
       foreach (
      $arr AS $key => $value)
       {
          
      $len strlen(trim($value));
          if (
      $len $max)
          {
              
      $max $len;
              
      $line $key 1;
          }
       }       
       
       echo 
      "Line $line is the longest with: $max chars.";

      ?>
      <?php unlink('World/Europe/Romania.country'); ?>

      Comment

      Working...
      X