# Find the longest string in a string (PHP version)

Posted by Cheez on Fri, 21 Feb 2020 14:13:10 +0100

Title Description 1: read in a string STR, and output the longest string in the string str. If there are more than one consecutive longest digital string, just output the first one.

• Input description

The input contains 1 test case, a string str, and the length does not exceed 255.

• Output description

Output the longest string of digits in str in a row.

• Example

Input is:
abcd12345ed125ss123456789

Output is:
The number string is 123456789 and the length is 9

• Train of thought analysis: separate the number string from the string, and calculate which number string has the longest length. What we need to do is to save the number string of each segment in a temporary string. If "the length of the current temporary string" is greater than "the length of the maximum number string", replace "the length of the maximum number string" with "the length of the current temporary string", clear the length of the current temporary string and start the next round of comparison.
• The implementation code is as follows
```<?php
function getLongNum(\$srcStr) {
\$arrRes = [
'str' => '',
'length' => 0
];
// If it is empty, return directly
if (empty(\$srcStr)) {
return \$arrRes;
}

// If it is a pure number, direct calculation
\$length = strlen(\$srcStr);
if (is_numeric(\$srcStr) && (strpos(\$srcStr, '.') === false)) {
\$arrRes = [
'str' => \$srcStr,
'length' => \$length,
];

return \$arrRes;
}

// Non pure number, need to calculate
\$tmp = '';
\$maxLength = 0;
\$maxStr = '';
for(\$i=0; \$i<\$length; \$i++) {
if(\$srcStr[\$i]>='0' && \$srcStr[\$i]<='9') {
while ((\$i < \$length) && (\$srcStr[\$i]>='0') && (\$srcStr[\$i]<='9')) {
\$tmp .= \$srcStr[\$i++];
}
\$tmpLength = strlen(\$tmp);
if (\$tmpLength > \$maxLength) {
\$maxLength = \$tmpLength;
\$maxStr = \$tmp;
}
}
\$tmp = '';
}

\$arrRes = [
'str' => \$maxStr,
'length' => \$maxLength,
];

return \$arrRes;
}

\$srcStr = '';
\$arrRes = getLongNum(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));

\$srcStr = '123456';
\$arrRes = getLongNum(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));

\$srcStr = '123456789.9876';
\$arrRes = getLongNum(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));

\$srcStr = 'abc12345ss789';
\$arrRes = getLongNum(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));

\$srcStr = 'aaaaaabbbb123';
\$arrRes = getLongNum(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));```

The operation results are as follows:

```array(2) { ["str"]=> string(0) "" ["length"]=> int(0) }
array(2) { ["str"]=> string(6) "123456" ["length"]=> int(6) }
array(2) { ["str"]=> string(9) "123456789" ["length"]=> int(9) }
array(2) { ["str"]=> string(5) "12345" ["length"]=> int(5) }
array(2) { ["str"]=> string(3) "123" ["length"]=> int(3) }```

From the result, it is in line with the expectation.

Topic Description 2: read in a string STR and output the longest string in the string str. If there are more than one consecutive longest digital string, the whole output.

• Input description

The input contains 1 test case, a string str, and the length does not exceed 255.

• Output description

Output the longest string of digits in str in a row.

• Example

Input is:
abcd987654321ed125ss123456789

Output is:
The digital string is 123456789, 987654321, and the length is 9

• The implementation code is as follows
```<?php
function getLongNumAll(\$srcStr) {
\$arrRes = [
'str' => [],
'length' => 0
];
// If it is empty, return directly
if (empty(\$srcStr)) {
return \$arrRes;
}

// If it is a pure number, direct calculation
\$length = strlen(\$srcStr);
if (is_numeric(\$srcStr) && (strpos(\$srcStr, '.') === false)) {
\$arrRes = [
'str' => [\$srcStr],
'length' => \$length,
];
return \$arrRes;
}

// Non pure number, need to calculate
\$tmp = '';
\$maxLength = 0;
for(\$i=0; \$i<\$length; \$i++) {
if(\$srcStr[\$i]>='0' && \$srcStr[\$i]<='9') {
while ((\$i < \$length) && (\$srcStr[\$i]>='0') && (\$srcStr[\$i]<='9')) {
\$tmp .= \$srcStr[\$i++];
}
\$tmpLength = strlen(\$tmp);
if (\$tmpLength > \$maxLength) {
\$maxLength = \$tmpLength;
\$arrRes['str'] = [\$tmp];
\$arrRes['length'] = \$tmpLength;
} elseif (\$tmpLength == \$maxLength) {
\$arrRes['str'][] = \$tmp;
}
}
\$tmp = '';
}

return \$arrRes;
}

\$srcStr = '123456789.987654321';
\$arrRes = getLongNumAll(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));

\$srcStr = 'abc12345ss7897654';
\$arrRes = getLongNumAll(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));

\$srcStr = 'aaaaaabbbbbbb';
\$arrRes = getLongNumAll(\$srcStr);
var_dump(\$arrRes);
print_r(nl2br("\n"));
?>```

The operation results are as follows:

```array(2) { ["str"]=> array(2) { [0]=> string(9) "123456789" [1]=> string(9) "987654321" } ["length"]=> int(9) }
array(2) { ["str"]=> array(1) { [0]=> string(7) "7897654" } ["length"]=> int(7) }
array(2) { ["str"]=> array(0) { } ["length"]=> int(0) }
```

From the result, it is in line with the expectation.

140 original articles published, 82 praised, 490000 visitors+

Topics: PHP