Link to home
Start Free TrialLog in
Avatar of mnb93
mnb93

asked on

Add $x in to $v every 10 charature. ($x changes every time)

Add $x in to $v every 10 charature. ($x changes every time)
Avatar of BogoJoker
BogoJoker

Hi mnb93,

I don't understand what you want.

Joe P
Avatar of mnb93

ASKER

dw I worked out what I needed.
Avatar of mnb93

ASKER

$d = '';
for($i=0;$i<strlen($x);$i++)
{
    if($i%10 == 0 AND $i !== 0)
    {
        //addchar
        $d .= $s.substr($x, $i, 1);
        $s = mkNews();
    }
    else
    {
        //do nothing
        $d .= substr($x, $i, 1);
    }
}

Points will go to anyone who can make something faster than that.
You could do:
if (strlen != 0)
{
    for($i=1;$i<strlen($x);$i++)
    {
        if($i%10 == 0)
        {
            //addchar
            $d .= $s.substr($x, $i, 1);
            $s = mkNews();
        }
        else
        {
            //do nothing
            $d .= substr($x, $i, 1);
        }
    }
}

This, although it looks larger eliminiates the check if i = 0 every single time.  It starts at 1 checking that the strlen() is in fact greater then 0.
Also let me write another one.
$length = strlen($x);
$pos = 0;
while($length > 10)
{
  $d .= substr($x, $pos, 9);
  $d .= $s.substr($x, $i, 1);
  $s = mkNews();
  $pos += 10;
  $length -= 10;
}
$d  .= substr($x, $pos);


This versino actually goes 10 letters at a time.  Works for $x string of any size it takes the 10 letters like so:
Add 9 to $d
Add $s and the 10th
Call mkNews()

Until there are less then 10 letters left to add, just add them onto the end.
Much faster cause your not looping letter by letter, your taking them 10 at a time.

Joe P
ASKER CERTIFIED SOLUTION
Avatar of BogoJoker
BogoJoker

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial