Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

PhP question..

  • 06-05-2006 01: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