Jump to content

Archived

This topic is now archived and is closed to further replies.

jcf

PHP question

Recommended Posts

This question was on an exam :

Write a PHP script that takes input as a data file with a list of
names and companies, everyline that begins with "S.L." is a company ,
every other line is a person (2nd and 1st name)

Re display the file with no companies and all the names reversed
eg

Murphy Paul
S.L. Microsoft
S.L. Sony
Henderson Bill
Jameson Harry
S.L. Xerox
Blair Andy

Becomes :
Paul Murphy
Bill Henderson
Harry Jameson
Andy Blair

So I wont bother putting my code in here (its awful as im new to PhP)
but i used the strpos() function to remove all the companies, and then an array
to swap around the 1st and last names ?

Can anyone else post code that would do the above ?









Share this post


Link to post
Share on other sites
Here's the solution I came up with:
[code]<?php
$in = file('exam.txt');
$out = array();
foreach($in as $line) {
    if (substr(trim($line),0,4) != 'S.L.') {
        $x = explode(' ',trim($line));
        $out[] = implode(' ',array_reverse($x));
        }
    }
$fp = f open('exam_out.txt','w');
f write($fp,implode("\n",$out). "\n");
f close($fp);
$results = file('exam_out.txt');
echo '<pre>' . print_r($results,true) . '</pre>';
?>[/code]

Note: remove the space from "f open", "f write", "f close"

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=371757:date=May 6 2006, 05:35 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 6 2006, 05:35 AM) [snapback]371757[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Here's the solution I came up with:
[code]<?php
$in = file('exam.txt');
$out = array();
foreach($in as $line) {
    if (substr(trim($line),0,4) != 'S.L.') {
        $x = explode(' ',trim($line));
        $out[] = implode(' ',array_reverse($x));
        }
    }
$fp = f open('exam_out.txt','w');
f write($fp,implode("\n",$out). "\n");
f close($fp);
$results = file('exam_out.txt');
echo '<pre>' . print_r($results,true) . '</pre>';
?>[/code]

Note: remove the space from "f open", "f write", "f close"

Ken
[/quote]

Wow!!!
That is hard to understand, I looked up the functions on php.net, but still its
tricky,
Any chance you could comment that ?

Share this post


Link to post
Share on other sites
You mean it's not self explanatory? :-)

[code]<?php
$in = file('exam.txt'); // read the whole file into an array
$out = array();          // initialize the array to hold the records to be written out
foreach($in as $line) { // go through the array line by line
    if (substr(trim($line),0,4) != 'S.L.') { // Only use those lines that don't start with "S.L."
        $x = explode(' ',trim($line));    // explode the trimmed line on a space to form an array with two elements. Trimming will remove the newline character
        $out[] = implode(' ',array_reverse($x)); // recreate a string with a space seperating the words after reversing the elements in the array
        }
    }
$fp = f open('exam_out.txt','w');
f write($fp,implode("\n",$out). "\n");   // write all the lines to a file, using implode to put a newline character between each element of the array.  And add a newline character to the end of the last line.
f close($fp);
$results = file('exam_out.txt');  // read all the lines back into another array
echo '<pre>' . print_r($results,true) . '</pre>';  // and display the results. Note you could also just display the contents of the $out array.
?>[/code]

Ken

Share this post


Link to post
Share on other sites
[!--quoteo(post=371770:date=May 6 2006, 07:11 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 6 2006, 07:11 AM) [snapback]371770[/snapback][/div][div class=\'quotemain\'][!--quotec--]
You mean it's not self explanatory? :-)

[code]<?php
$in = file('exam.txt'); // read the whole file into an array
$out = array();          // initialize the array to hold the records to be written out
foreach($in as $line) { // go through the array line by line
    if (substr(trim($line),0,4) != 'S.L.') { // Only use those lines that don't start with "S.L."
        $x = explode(' ',trim($line));    // explode the trimmed line on a space to form an array with two elements. Trimming will remove the newline character
        $out[] = implode(' ',array_reverse($x)); // recreate a string with a space seperating the words after reversing the elements in the array
        }
    }
$fp = f open('exam_out.txt','w');
f write($fp,implode("\n",$out). "\n");   // write all the lines to a file, using implode to put a newline character between each element of the array.  And add a newline character to the end of the last line.
f close($fp);
$results = file('exam_out.txt');  // read all the lines back into another array
echo '<pre>' . print_r($results,true) . '</pre>';  // and display the results. Note you could also just display the contents of the $out array.
?>[/code]

Ken
[/quote]

ok 1 more question, why here :
if (substr(trim($line),0,4) != 'S.L.')

why do you use 0,4 as an argument in substr() ?

Share this post


Link to post
Share on other sites
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]ok 1 more question, why here :
if (substr(trim($line),0,4) != 'S.L.')

why do you use 0,4 as an argument in substr() ?[/quote]

From the [a href=\"http://www.php.net/substr\" target=\"_blank\"]manual page[/a]:
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--] substr() returns the portion of string specified by the start and length parameters.

If start is non-negative, the returned string will start at the start'th position in string, counting from zero. For instance, in the string 'abcdef', the character at position 0 is 'a', the character at position 2 is 'c', and so forth.[/quote]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.