计算数组上的字符串长度(strlen),返回小于最大字符数的索引

时间:2018-04-13 19:44:36

标签: php string

必须有一种更简单/自动的方法吗?我需要保留^^分隔符,不超过200个字符。

<?php

$string = 'Lousaxafro pi Lowaxaskaxafro.^^Kuaxalaxankoow te dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi whelsopewol te yeep ockino.^^Tep eb zo rino axail birtols wirr losurt din fottol buor obbicioncupp.^^Kivo yeep mohicro pit fottol evolaxarr dolbelmaxanco axas worr axas up axawosemo axagglossivo seuctick ockino.^^Oaxasupp te dinchaxarr!';
$stringlen = strlen($string);
echo $stringlen;

if ($stringlen > 200) {
$explode = explode('^^', $string);
    echo '<pre>';
    print_r($explode);
    echo '</pre>';
        $zero = strlen($explode[0]);
    echo $zero . '<br />';
    $one = strlen($explode[0]) + strlen($explode[1]);
    echo $one . '<br />';
        $two = strlen($explode[0]) + strlen($explode[1]) + strlen($explode[2]);
    echo $two . '<br />';
        $three = strlen($explode[0]) + strlen($explode[1]) + strlen($explode[2]) + strlen($explode[3]);
    echo $three . '<br />';
        $four = strlen($explode[0]) + strlen($explode[1]) + strlen($explode[2]) + strlen($explode[3]) + strlen($explode[4]);
    echo $four . '<br /><br />';
    foreach ($explode as $x) {
        echo strlen($x) . '<br />';
    }
}

输出:

349
Array
(
    [0] => Lousaxafro pi Lowaxaskaxafro.
    [1] => Kuaxalaxankoow te dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi whelsopewol te yeep ockino.
    [2] => Tep eb zo rino axail birtols wirr losurt din fottol buor obbicioncupp.
    [3] => Kivo yeep mohicro pit fottol evolaxarr dolbelmaxanco axas worr axas up axawosemo axagglossivo seuctick ockino.
    [4] => Oaxasupp te dinchaxarr!
)

29 // [0] length
138 // [0] + [1] length
208 // [0]  + [1] + [2] length
318 // [0]  + [1] + [2] + [3] length
341 // [0]  + [1] + [2] + [3] + [4] length

29 // [0] length
109 // [1] length
70 // [2] length
110 // [3] length
23 // [4] length

我显然不知道如何动态地执行此操作..这是一个解决方案,但它不考虑数组的count以及可能的添加数量。

if ($stringlen > 200) {
$explode = explode('^^', $string);
    echo '<pre>';
    print_r($explode);
    echo '</pre>';

        if (strlen($explode[0]) + strlen($explode[1]) + strlen($explode[2]) + strlen($explode[3]) + strlen($explode[4]) < 192) {
            echo 'five - [0]+[1]+[2]+[3]+[4]' . '<br />::';
            $goodlength = $explode[0] . '^^' . $explode[1] . '^^' . $explode[2] . '^^' . $explode[3] . '^^' . $explode[4];
            }
        elseif (strlen($explode[0]) + strlen($explode[1]) + strlen($explode[2]) + strlen($explode[3]) < 194) {
            echo 'four - [0]+[1]+[2]+[3]' . '<br />::';
            $goodlength = $explode[0] . '^^' . $explode[1] . '^^' . $explode[2] . '^^' . $explode[3];
        }
        elseif (strlen($explode[0]) + strlen($explode[1]) + strlen($explode[2]) < 196) {
            echo 'three - [0]+[1]+[2]' . '<br />::';
            $goodlength = $explode[0] . '^^' . $explode[1] . '^^' . $explode[2];
        }
        elseif (strlen($explode[0]) + strlen($explode[1]) < 198) {
            echo 'two - [0]+[1]' . '<br />::';
            $goodlength = $explode[0] . '^^' . $explode[1];
        }
        elseif (strlen($explode[0]) < 200) {
            echo 'on - [0]' . '<br />::';
            $goodlength = $explode[0];
        }

echo '<br />' .  $goodlength . '<br />';

}

输出:

  

两个 - [0] + [1] :: Lousaxafro pi Lowaxaskaxafro。^^ Kuaxalaxankoow te   dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi   whelsopewol te yeep ockino。

我也尝试过循环

$i = 0;
$length = '';
if ($stringlen > 200) {
$explode = explode('^^', $string);
    foreach ($explode as $x) {
        echo $i . '<br />';
        $length .= + strlen($x);
                    echo '<br />';
        if ($length > 200) {
            echo '<br />';
            break;
        }
                $i++;
    }
}

输出:

  

0

     

1

但这仍然不是一个可行的解决方案。任何有好主意的人?

3 个答案:

答案 0 :(得分:2)

您可以使用strpos()检查序列^^的下一个位置,如果大小小于200,则连接到输出字符串,否则打破循环:

$string = 'Lousaxafro pi Lowaxaskaxafro.^^Kuaxalaxankoow te dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi whelsopewol te yeep ockino.^^Tep eb zo rino axail birtols wirr losurt din fottol buor obbicioncupp.^^Kivo yeep mohicro pit fottol evolaxarr dolbelmaxanco axas worr axas up axawosemo axagglossivo seuctick ockino.^^Oaxasupp te dinchaxarr!';

$out = '';
while (true) {
    // get the next position of ^^
    $pos = strpos($string, '^^');
    // if not found, or if the current output + next part > 200: break
    if ($pos === false || $pos + strlen($out) > 200) {
        // for the last part, remove the trailing ^^
        if ($pos !== false && $out != '') {
            $out = substr($out, 0, -2);
        }
        break ;
    }
    // append the current part to the output
    $out .= substr($string, 0, $pos + 2);
    // reduce the current string
    $string = substr($string, $pos + 2);
}
echo $out;

输出:

  

Lousaxafro pi Lowaxaskaxafro。^^ Kuaxalaxankoow te dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi whelsopewol te yeep ockino。

答案 1 :(得分:1)

如何动态执行此操作:

迭代字符串的爆炸部分,直到添加下一部分将结果的长度超过200.

$len = 0;
foreach (explode('^^', $string) as $i => $section) {
    if (($len += strlen($section)) + $i * 2 > 200) break;
    $result[] = $section;
}
echo implode('^^', $result);

+ $i * 2说明了结果中分隔符的总长度。

答案 2 :(得分:0)

您可以计算字符串的长度,并通过嵌套foreachfor循环来添加数组开头的每个字符串长度。 例如:

<?php
$string = ...;

$parts = explode('^^',$string);
$length = [];

foreach($parts as $index => $value) {
   $length[$index] = 0;
   for($i = 0;$i < $index; $i++) {
       $length[$index] += strlen($parts[$i]);
   }
}

print_r($parts);
print_r($length);