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)
Started on the old forum in January 2012 · view the original in the archive · old links redirect here
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)
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.";
?>