jcf Posted May 6, 2006 Share Posted May 6, 2006 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. MicrosoftS.L. SonyHenderson Bill Jameson HarryS.L. XeroxBlair AndyBecomes : Paul MurphyBill HendersonHarry JamesonAndy BlairSo 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 ? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 6, 2006 Share Posted May 6, 2006 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 Link to comment Share on other sites More sharing options...
jcf Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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 ? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 6, 2006 Share Posted May 6, 2006 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 outforeach($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 arrayecho '<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 Link to comment Share on other sites More sharing options...
jcf Posted May 6, 2006 Author Share Posted May 6, 2006 [!--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 outforeach($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 arrayecho '<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() ? Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted May 7, 2006 Share Posted May 7, 2006 [!--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 Quote Link to comment Share on other sites More sharing options...
jcf Posted May 7, 2006 Author Share Posted May 7, 2006 ok sorry i get it now. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.