2

I would like to extract the last 4 words from a string as a single chunk.

For example, if I have:

"Are You Looking For The Best Website Review"

I'd like to catch the last four words as:

"The Best Website Review"

I only have basic coding knowledge and have tried every variation I could find within this forum.

The closest I've come is by using the suggestion of Rick Kuipers (How to obtain the last word of a string) but this gives me the words as individual values.

$split = explode(" ", $str);

echo $split[count($split)-4];
echo $split[count($split)-3];
echo $split[count($split)-2];
echo $split[count($split)-1];

My coding knowledge is limited so any suggestions would be appreciated.

Community
  • 1
  • 1
Codex
  • 23
  • 2

2 Answers2

3

Simply use str_word_count along with the array_splice like as

$str = "Are You Looking For The Best Website Review";
$arr = str_word_count($str,1);
echo implode(' ',array_splice($arr,-4));

Edited

If your text contains Are-You-Looking-For-The-Best-Website-Review hiphen the you can use str_replace like as

$arr = str_word_count(str_replace('-',' ',$str),1);
echo implode(' ',array_splice($arr,-4));

Demo

Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
  • 1
    Thank you for such a quick reply. This works perfectly. However I forgot to mention that one version of my titles contains hypens i.e Are-You-Looking-For-The-Best-Website-Review. Is it possible to adjust your code to work with these? – Codex Nov 28 '15 at 11:42
  • Updated my answer @Codex – Narendrasingh Sisodia Nov 28 '15 at 12:07
  • 1
    Uchiha, that's perfect. I can't ask for anymore. Again, thank you for sharing your valuable knowledge. – Codex Nov 28 '15 at 13:18
2

You can use a simple regular expression for this:

<?php

$subject = "Are You Looking For The Best Website Review";
$pattern = '/(\w+\s+\w+\s+\w+\s+\w+)\s*$/u';
preg_match($pattern, $subject, $tokens);

var_dump($tokens[0]);

The output is:

string(23) "The Best Website Review"

This also has the advantage that the type and number of whitespaces between words is irrelevant and trailing whitespaces at the end of the string are ignored.


Edit: considering your comment to the answer posted by @Uchiha this is a variant of the pattern that shows how you can easily match words delimited by other characters apart from whitespaces:

$pattern = '/(\w+[-\s]+\w+[-\s]+\w+[-\s]+\w+)\s*$/u';
arkascha
  • 41,620
  • 7
  • 58
  • 90
  • arkascha, also thanks for the quick solution. I can't believe how quickly this problem was solved. – Codex Nov 28 '15 at 11:54
  • @Codex I added a variant of the pattern that shows how you can easily match words delimited by other characters than whitespaces. Hope this helps. And frankly, I have no idea why one should prefer the more complex and less efficient approach using the combination of normal string methods... – arkascha Nov 28 '15 at 12:09
  • Arkascha, thanks again for taking the time provide an additional refinement to your code. This has been a very positive outcome to something I thought would take much longer to achieve. – Codex Nov 29 '15 at 09:23
  • Great if we could help with this. You should then accept an answer if your issue has been solved. Have fun! – arkascha Nov 29 '15 at 09:25
  • I liked your solution from the beginning only+1 – Narendrasingh Sisodia Nov 30 '15 at 02:36