-2

Let's say I have the string below

$string = 'PHP Coding.
Hello World!
Merry Christmas!
Happy New Year!
Merry Super Early Next Christmas?
I want Pizza, and Cake
Hehehe
Hohohoho';

How do I get the last n sentences from the string, for example, the last 3 sentences, which should give the following output:

I want Pizza, and Cake
Hehehe
Hohohoho

Edit: I'm using data from sql

Aesreal
  • 131
  • 1
  • 12
  • 7
    split on linebreaks, giving you an array, then pop however many lines you want from the end of the array. – Marc B Jan 07 '15 at 14:28
  • php explode() function should help you split the string. I would suggest reading the PHP string manual though before asking for help. – MadDokMike Jan 07 '15 at 14:30
  • 1
    @MarcB Sure, if there were line breaks to start with. OP's code is set inside a variable producing `PHP Coding. Hello World! Merry Christmas! Happy New Year! Merry Super Early Next Christmas? I want Pizza, and Cake Hehehe Hohohoho` – Funk Forty Niner Jan 07 '15 at 14:31
  • Please check this link: http://stackoverflow.com/questions/10494176/explode-a-paragraph-into-sentences-in-php – besciualex Jan 07 '15 at 14:35
  • 1
    @fred: there's line breaks. it's a multi-line string definition, after all. – Marc B Jan 07 '15 at 14:37
  • @MarcB Multi-line definition, yes I agree. I might have taken your initial comment out of context. However, to me, a line break consists of the ever so famous hidden `\n` which isn't present in OP's long string. – Funk Forty Niner Jan 07 '15 at 14:42
  • 1
    then it'd have to be `$foo = "bar\nbaz"`. – Marc B Jan 07 '15 at 14:43

2 Answers2

3

This should work for you:

<?php

    $string = 'PHP Coding.
            Hello World!
            Merry Christmas!
            Happy New Year!
            Merry Super Early Next Christmas?
            I want Pizza, and Cake
            Hehehe
            Hohohoho';

    list($sentence[], $sentence[], $sentence[]) = array_slice(explode(PHP_EOL, $string), -3, 3);

    print_r($sentence);

?>

Output:

Array ( [2] => Hohohoho [1] => Hehehe [0] => I want Pizza, and Cake )

EDIT :

Here you can define how many sentence you want from the back:

<?php

    $string = 'PHP Coding.
            Hello World!
            Merry Christmas!
            Happy New Year!
            Merry Super Early Next Christmas?
            I want Pizza, and Cake
            Hehehe
            Hohohoho';

    $n = 3;

    $sentence = array_slice(explode(PHP_EOL, $string), -($n), $n);
    $sentence = array_slice(explode(PHP_EOL, nl2br($string)), -($n), $n); // Use this for echoing out in HTML
    print_r($sentence);

?>

Output:

Array ( [0] => I want Pizza, and Cake [1] => Hehehe [2] => Hohohoho )
Aesreal
  • 131
  • 1
  • 12
Rizier123
  • 58,877
  • 16
  • 101
  • 156
  • are there any ways to do them in a single line? – Aesreal Jan 07 '15 at 14:32
  • *"are there any ways to do them in a single line?"* - Too easy. – Funk Forty Niner Jan 07 '15 at 14:37
  • @Aesreal `foreach($sentence as $var){ echo $var . " "; }` producing `I want Pizza, and Cake Hehehe Hohohoho` – Funk Forty Niner Jan 07 '15 at 14:45
  • @Aesreal What do you mean with: `a single line`? You can write your entire script on one line! – Rizier123 Jan 07 '15 at 14:47
  • *Hm,* I was under the impression OP wanted the output to be on a single line. – Funk Forty Niner Jan 07 '15 at 14:52
  • @Rizier123 It's still not working thou, I've tried using nl2br($string) instead. this data is actually obtained from sql, and as such it is as Fred -ii said, PHP_EOL is not working – Aesreal Jan 07 '15 at 14:53
  • @Fred-ii- yup, i wanted them to be in a single line like what you did, but if that is not possible then a function is also fine, just that none of the answers are working right now! – Aesreal Jan 07 '15 at 14:54
  • 1
    @Aesreal What Rizier originally posted as an answer and adding my `foreach` should have worked. You may have to update your question with the SQL you're using in order to achieve your goal. By "not working"; can you define that? Are you getting errors? – Funk Forty Niner Jan 07 '15 at 14:56
  • 1
    Ahhhh, its working now! had some syntax problems with it. =.= sorry for wasting your time, and thanks for the help! – Aesreal Jan 07 '15 at 14:58
0
$string = 'PHP Coding.
Hello World!
Merry Christmas!
Happy New Year!
Merry Super Early Next Christmas?
I want Pizza, and Cake
Hehehe
Hohohoho';



function getLast($string, $n){
    $splits = explode(PHP_EOL, $string);
    return array_slice($splits, -$n, count($splits));
}

$result = getLast($string, 2);
var_dump($result);
Spoke44
  • 968
  • 10
  • 24