Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

explain how this code can create intended output

<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28287733.html

// SIMULATE READING THE EXTERNAL FILE WITH file_get_contents();
$doc = <<<EOD
DTw  DTw  3658976250  staff3
DTw  DTw  3658422687  staff3
DTw  DTw  3653399588  staff3
DTw  DTw  3653174764  staff3
DTw  DTw  3546098525  staff3
DTw  DTw  3545571594  staff3
DTw  DTw  3545571593  staff3
DTw  DTw  3540574071  staff3
DTw  DTw  3540454344  staff3
DTw  DTw  3540376883  staff3
fre  fre  3544889093  staff3
gam  gam  3955910171  staff3
gam  gam  3955910165  staff3
gam  gam  3649458031  staff3
gam  gam  3648816297  staff3
gam  gam  3648783076  staff3
gam  gam  3648516109  staff3
gam  gam  3647758123  staff3
gam  gam  3554774961  staff3
gam  gam  3540425395  staff3
gam  gam  3540357159  staff3
gam  gam  3540345288  staff3
h1a  staff3  3700557963  staff3
h1a  h1a  3693946789  staff3
h1a  h1a  3693806133  staff3
h1a  staff3  3684800156  staff3
h1a  h1a  3684173904  staff3
h1a  h1a  3682095987  staff3
h1a  h1a  3682095983  staff3
h1a  h1a  3542388954  staff3
nyh  nyh  3554817690  staff3
nyh  nyh  3554796334  staff3
nyh  nyh  3554701760  staff3
nyh  nyh  3554689575  staff3
nyh  nyh  3541014726  staff3
nyh  nyh  3541012663  staff3
nyh  nyh  3540619436  staff3
nyh  nyh  3540474531  staff3
EOD;

// BREAK THE DOCUMENT INTO INDIVIDUAL LINES
$arr = explode(PHP_EOL , $doc);

// PROCESS EACH LINE
$out = array();
foreach ($arr as $str)
{
    // ELIMINATE UNWANTED WHITESPACE
    $str = preg_replace('/\s\s+/', ' ', $str);
    $str = trim($str);

    // RECONSTRUCT INTO THE QUERY FORMAT
    $dat = explode(' ', $str);
    $out[] = "('" . implode("', '", $dat) . "')";
}

// BUILD THE QUERY STRING
$new = implode(',' . PHP_EOL, $out);
echo $new;

Open in new window



please explain to me why is code works

I understand turning each line into array element
but I do not understand replacing spaces and then how do the commas and quotes get added
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America 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
Also, if you want to learn more about regular expressions and test them out yourself, try downloading the free tool called The Regex Coach. I use it all the time when I'm working with really complex regular expressions. It highlights and shows you how the regular expressions are working.
No points for this, but if you wanted to spend $2 on your career, this would be a good investment:
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/
Avatar of rgb192

ASKER

I do not understand implode $dat.  Which $dat.  Maybe forloop $dat[1].
SOLUTION
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
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
Avatar of rgb192

ASKER

I do not understand
(
)

$out[] = "('" . implode("', '", $dat) . "')";

I think that
left (
and
right )
would appear many times
because of loop:
foreach ($arr as $str)
SOLUTION
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
Avatar of rgb192

ASKER

gr8gonzo final expert comment shows me that
(
)
is from many rows


Thanks