Reputation: 913
There must be an easier/automatic way to do this? I need to keep the ^^
delimiter and under 200 characters.
<?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 />';
}
}
This outputs:
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
I obviously have no idea how to dynamically do this.. here is one solution, but it doesn't account for the count
of the array and how many possible additions.
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 />';
}
This outputs:
two - [0]+[1] :: Lousaxafro pi Lowaxaskaxafro.^^Kuaxalaxankoow te dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi whelsopewol te yeep ockino.
I've also tried with loops
$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++;
}
}
Outputs:
0
1
But this still does not seem like a viable solution. Anyone with any good ideas?
Upvotes: 1
Views: 215
Reputation: 41810
How to do this dynamically:
Iterate the exploded sections of the string until adding the next section will push the length of the result over 200.
$len = 0;
foreach (explode('^^', $string) as $i => $section) {
if (($len += strlen($section)) + $i * 2 > 200) break;
$result[] = $section;
}
echo implode('^^', $result);
+ $i * 2
accounts for the combined length of the delimiters in the result.
Upvotes: 1
Reputation: 19780
You could use strpos()
to check to next position of the sequence ^^
, concatenate to the outputs string if the size is lesser than 200, and break the loop otherwise:
$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;
Outputs:
Lousaxafro pi Lowaxaskaxafro.^^Kuaxalaxankoow te dincloaxaso yeep axaccorolaxatien fupp axaghtick velo telquo pi whelsopewol te yeep ockino.
Upvotes: 2
Reputation: 3967
you can count the length of the string and add every string length from the beginning of the array by nesting foreach
and for
loop.
example :
<?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);
Upvotes: 0