[thelist] PHP Challenge

Douglas Clifton dwclifton at gmail.com
Sat Nov 26 17:13:26 CST 2005


Matt,

Why use preg_split()? PHP strings *are* arrays of characters!

Being something of a minimalist:

function interleaveStrings($a, $b)  {

    $max = max($al = strlen($a), $bl = strlen($b));

    for ($i = 0; $i < $max; $i++)  {

        if ($i < $al) $r .= $a[$i];
        if ($i < $bl) $r .= $b[$i];
    }
    return $r;
}

Probably more efficient would be to interleave the shorter
length, and then concatenate the remainder of the longer.

 --doug

> From: Matt Warden <mwarden at gmail.com>
> Subject: Re: [thelist] PHP Challenge
> To: "thelist at lists.evolt.org" <thelist at lists.evolt.org>

> Interesting problem.
>
> Run:
> http://mwarden.f2o.org/sandbox/interleaveStrings.php
>
> Source:
> http://mwarden.f2o.org/sandbox/viewsource.php?linemode=0&f=interleaveStrings.php
[snip]

--
Douglas Clifton
dwclifton at gmail.com
http://loadaveragezero.com/
http://loadaveragezero.com/app/s9y/
http://loadaveragezero.com/drx/rss/recent


More information about the thelist mailing list