Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 273
  • Last Modified:

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
0
rgb192
Asked:
rgb192
  • 4
  • 3
  • 2
4 Solutions
 
gr8gonzoConsultantCommented:
Sure. So all this happens inside the loop. First you have this line of code:
$str = preg_replace('/\s\s+/', ' ', $str);

Open in new window

This is a regular expression that is broken down into FIND and REPLACE. The first part is the find command:

/\s\s+/

This is broken down like this:

/ = Start of the regular expression
\s = Any kind of space character (like a tab character or a regular space)
\s+ = Another space character and any more space characters afterwards
/ = End of the regular expression

So if you had a line that looked like this:

My(space)friend(space)(space)(space)(tab)has(tab)(tab)3(space)fingers.

...then the regular expression would match two spots:
(space)(space)(space)(tab)
(tab)(tab)

It would then go to the next part of the preg_replace, the REPLACE part. So any time it finds a match, it replaces the match with a single space character: ' '

So the final result looks like:
My(space)friend(space)has(space)3(space)fingers.

This:
$str = trim($str);

Open in new window

...just removes any extra space from the beginning and end of the line.

So let's say you entered the loop and you were processing this line:
$str = 'h1a  staff3  3684800156  staff3(line break)
';

After the above two lines, you should have:
$str = 'h1a staff3 3684800156 staff3';

So now the next bit of code turns the line into an array using the space as a delimiter:
$dat = explode(' ', $str);

Open in new window


Now $dat should be an array containing:
[0] = h1a
[1] = staff3
[2] = 368400156
[3] = staff3

Finally, the implode() combines the array elements back into an array with commas and quotes and puts quotes and parentheses around it:
$out[] = "('" . implode("', '", $dat) . "')";
...or if we separate each piece onto its own line for readability...
$out[] = "('" .
             implode("', '", $dat) .
             "')";

Open in new window


The implode() will change $dat back to a string, like this:
h1a', 'staff3', '368400156', 'staff3

So you need to add quotes to the beginning and end (and the parentheses):
('h1a', 'staff3', '368400156', 'staff3')

... and then the result goes into an array called $out.

So you eventually have an array called $out like this:

[0] = ('h1a', 'staff3', '368400156', 'staff3')
[1] = ('nyh', 'nyh', '3554701760', 'staff3')
..etc...

Then the final implode() joins all those items together with a comma and EOL in-between them:
$new = implode(',' . PHP_EOL, $out);

('h1a', 'staff3', '368400156', 'staff3'),
('nyh', 'nyh', '3554701760', 'staff3')
0
 
gr8gonzoConsultantCommented:
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.
0
 
Ray PaseurCommented:
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/
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
rgb192Author Commented:
I do not understand implode $dat.  Which $dat.  Maybe forloop $dat[1].
0
 
Ray PaseurCommented:
There is no "forloop" in PHP.  Implode() is documented here:

http://php.net/implode

Any PHP function can be looked up by typing its name after http://php.net/
0
 
gr8gonzoConsultantCommented:
implode() simply joins all of the elements of an array together using a string that you've specified. For example:

$myArray = array("rgb192","Ray_Paseur","gr8gonzo");

echo implode("___",$myArray);   // rgb192___Ray_Paseur___gr8gonzo

echo implode(" ANY STRING CAN BE A DELIMITER ",$myArray);   // rgb192 ANY STRING CAN BE A DELIMITER Ray_Paseur ANY STRING CAN BE A DELIMITER gr8gonzo

Open in new window

0
 
rgb192Author Commented:
I do not understand
(
)

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

I think that
left (
and
right )
would appear many times
because of loop:
foreach ($arr as $str)
0
 
gr8gonzoConsultantCommented:
Yes, you should see a ( at the beginning and ) at the end of each line of data, like this:

...
('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'),
...


Are you seeing something different? Or are you saying that you don't want the ( and ) on each line ?
0
 
rgb192Author Commented:
gr8gonzo final expert comment shows me that
(
)
is from many rows


Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now