HELP! php function - count string

Started on the old forum in January 2012 · view the original in the archive · old links redirect here

2 replies · 360 views · started 14 years ago · 1 min read

3 participants
Jerico Pascual Newbie · 1 posts #1

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)

arnage Lead · 811 posts #2

Use maths max() function.

<!DOCTYPE html PUBLIC "-//***WAPFORUM**.**RS***
i0nutzxp Developer · 150 posts #3

Here's an ugly "algorithm":

<?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'); ?>

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close