Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

explain how this code can create intended output

Posted on 2013-11-11
9
Medium Priority
?
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 35

Accepted Solution

by:
gr8gonzo earned 1500 total points
ID: 39639748
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 35

Expert Comment

by:gr8gonzo
ID: 39639751
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 111

Expert Comment

by:Ray Paseur
ID: 39639821
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:rgb192
ID: 39647878
I do not understand implode $dat.  Which $dat.  Maybe forloop $dat[1].
0
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 39647953
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 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1500 total points
ID: 39649077
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
ID: 39651091
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 35

Assisted Solution

by:gr8gonzo
gr8gonzo earned 1500 total points
ID: 39651256
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
ID: 39653172
gr8gonzo final expert comment shows me that
(
)
is from many rows


Thanks
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

688 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