How i get last word

Discussion in 'PHP' started by ironmankho, Jul 25, 2013.

  1. #1
    HI everyone
    I want to get string last word i.e Programming Languages

    Books > New, Used & Rental Textbooks > Computer Science > Programming Languages

    is there any easy solution i tried explode but did not understand how it work properly because i have other strings

    Books > Computers & Technology > Computer Science
    Books > Computers & Technology > Databases
    Books > Computers & Technology > Hardware > Design & Architecture
    Books > Computers & Technology > Programming > Languages & Tools > Lisp
    Books > Computers & Technology > Software
    Books > Education & Reference
    Books > New, Used & Rental Textbooks > Computer Science > Database Storage & Design

    My target is getting
    Computer Science
    Databases
    Design & Architecture
    Lisp
    Software
    Education & Reference
    Database Storage & Design
     
    Solved! View solution.
    ironmankho, Jul 25, 2013 IP
  2. GMF

    GMF Well-Known Member

    Messages:
    855
    Likes Received:
    113
    Best Answers:
    19
    Trophy Points:
    145
    #2
    Mhhh... let's see...I assume that every line is stored in an array

    
    $library = array(
    "Books > Computers & Technology > Computer Science",
    "Books > Computers & Technology > Databases",
    "Books > Computers & Technology > Hardware > Design & Architecture",
    "Books > Computers & Technology > Programming > Languages & Tools > Lisp",
    "Books > Computers & Technology > Software",
    "Books > Education & Reference",
    "Books > New, Used & Rental Textbooks > Computer Science > Database Storage & Design"
    );
    foreach($library as $string)
    {
         $split = explode(" > ", $string);
    
         echo $split[count($split)-1]."<br />";
    }
    
    PHP:

    Stackoverflow helped a lot
    http://stackoverflow.com/questions/11029447/how-to-get-last-string-in-sentences-using-php
    Code (markup):
     
    GMF, Jul 26, 2013 IP
  3. ironmankho

    ironmankho Active Member

    Messages:
    393
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #3
    hi your help is appropriated but how i can convert all string into one variable when out put

    Computer Science
    Databases
    Design & Architecture
    Lisp
    Software
    Education & Reference
    Database Storage & Design

    like
    $a='Computer Science,Databases,Design & Architecture,Lisp,Software,Education & Reference,Database Storage & Design';
     
    ironmankho, Jul 26, 2013 IP
  4. #4
    Mhhh...

    
    $section = array();
    $library = array(
    "Books > Computers & Technology > Computer Science",
    "Books > Computers & Technology > Databases",
    "Books > Computers & Technology > Hardware > Design & Architecture",
    "Books > Computers & Technology > Programming > Languages & Tools > Lisp",
    "Books > Computers & Technology > Software",
    "Books > Education & Reference",
    "Books > New, Used & Rental Textbooks > Computer Science > Database Storage & Design"
    );
    foreach($library as $string)
    {
        $split = explode(" > ", $string);
        $section[] = $split[count($split)-1];
    }
     
    $result = implode(", ", $section);
    echo $result;
    
    PHP:
    Which results in this

    Computer Science, Databases, Design & Architecture, Lisp, Software, Education & Reference, Database Storage & Design
    Code (markup):
     
    GMF, Jul 26, 2013 IP
  5. Ronnie403

    Ronnie403 Active Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    80
    Digital Goods:
    8
    #5
    Simpler
    $str = explode(' ',$your_string);
    echo end($str);
    PHP:
    Oh i didnt know it was an array, but this will get the last word in a sentance etc...
    Will leave it may be useful for someone else.
     
    Ronnie403, Jul 26, 2013 IP
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    @ronnie403 -- look at the data closer, they don't want just the last WORD, they want the last SECTION. (though the OP's broken Engrish is most likely to blame for your thinking such) -- Doesn't want just "Science", wants "computer science", doesn't want just "Design", they want "Database Storage & Design"

    GMF's answer is ok, but would benefit from your suggestion of 'end' as well as building the comma delimited list inside the loop instead of with implode.

    $library = array(
    	'Books > Computers & Technology > Computer Science',
    	'Books > Computers & Technology > Databases',
    	'Books > Computers & Technology > Hardware > Design & Architecture',
    	'Books > Computers & Technology > Programming > Languages & Tools > Lisp',
    	'Books > Computers & Technology > Software',
    	'Books > Education & Reference',
    	'Books > New, Used & Rental Textbooks > Computer Science > Database Storage & Design'
    );
    $result='';
    foreach ($library as $string) $result .= end(explode(' > ', $string)).', ';
     
    echo trim($result,', ');
    Code (markup):
    ... and of course my nitpick of getting rid of the stupid malfing double quotes :p
     
    deathshadow, Jul 27, 2013 IP
    GMF likes this.
  7. Ronnie403

    Ronnie403 Active Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    80
    Digital Goods:
    8
    #7
    Lol i was gonna post the same thing you posted after i realised it in my first post, but you already posted it :p
     
    Ronnie403, Jul 27, 2013 IP