Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PhP question..

Options
  • 06-05-2006 1:13am
    #1
    Closed Accounts Posts: 289 ✭✭


    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 ?


Comments

  • Closed Accounts Posts: 2,046 ✭✭✭democrates


    At this stage you've sorted reading line by line from the file and excluding lines beginning with S.L. so all you need is to reverse the names on remaining lines.

    Within your loop:

    [PHP]$name = explode(" ",$line); // create an array using space as a delimeter
    echo $name[1]." ".$name[0]; //output firstname, space, lastname[/PHP]


Advertisement