Solved

explain how this code can create intended output

Posted on 2013-11-11
9
261 Views
Last Modified: 2013-11-16
<?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
Comment
Question by:rgb192
  • 4
  • 3
  • 2
9 Comments
 
LVL 34

Accepted Solution

by:
gr8gonzo earned 375 total points
Comment Utility
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
 
LVL 34

Expert Comment

by:gr8gonzo
Comment Utility
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
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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
 

Author Comment

by:rgb192
Comment Utility
I do not understand implode $dat.  Which $dat.  Maybe forloop $dat[1].
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 125 total points
Comment Utility
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
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 375 total points
Comment Utility
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
 

Author Comment

by:rgb192
Comment Utility
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
 
LVL 34

Assisted Solution

by:gr8gonzo
gr8gonzo earned 375 total points
Comment Utility
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
 

Author Closing Comment

by:rgb192
Comment Utility
gr8gonzo final expert comment shows me that
(
)
is from many rows


Thanks
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now