2

I am trying to get the last few words in a string. For the last word, I am using this:

$string = "Hallo dies ist ein extrem langer Text den ich gern gekuerz haette, leider bin ich selbst zu doof dafür und mach hier einen test.";

$pattern = '/[^ ]*$/';

preg_match($pattern, $string, $result);

echo "<br>The last word is:------ ". $result[0] ." ---------<br>";

It works fine, but I need the flexibility to get, say, the last 3 words.

I do not know how to change the pattern.

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
hamburger
  • 1,339
  • 4
  • 20
  • 41
  • Do you want the last three words as a single string, or the last three words as three separate elements? Your [mcve] is not crystal clear on the desired result. – mickmackusa Jan 24 '23 at 04:45

5 Answers5

1

It's probably better off using explode on the string, something like this:

$string = 'Hello, my name is Jordan Doyle';
$string_array = explode(' ', $string);
echo end($string_array);

Example output:

root@upstairslinux:~# php example.php
Doyle

root@upstairslinux:~#

Here's a function to get a specified amount of lines...

<?php
function get_last_words($amount, $string)
{
    $string_array = explode(' ', $string);

    return array_slice($string_array, count($string_array) - $amount);
}

$string = 'Hello, my name is Jordan Doyle';
var_dump(get_last_words(3, $string));

Example output:

root@upstairslinux:~# php example.php
array(3) {
  [0]=>
  string(2) "is"
  [1]=>
  string(6) "Jordan"
  [2]=>
  string(5) "Doyle"
}

root@upstairslinux:~#
Jordan Doyle
  • 2,976
  • 4
  • 22
  • 38
  • I'am working with this: $length = 3; $shortStart = implode(' ',array_slice(str_word_count($string,1),0,$length)); echo "
    shortened:------ ". $shortStart ." ---------
    "; $shortened = implode(' ',array_slice(str_word_count($string,1),count(str_word_count($string,1))-$length)); echo "
    shortened:------ ". $shortened ." ---------
    "; It works fine but I'am looking for pattern now, to built in for a function.
    – hamburger Mar 08 '13 at 17:04
  • Edited my post to include this for you. – Jordan Doyle Mar 08 '13 at 17:20
1

That will do the job

$pattern = '/(([^ ]*)\s+([^ ]*)\s+([^ ]*))$/';

Example

or

$pattern = '/((([^ ]*)[\s.]+){3})$/';

Example

peterm
  • 91,357
  • 15
  • 148
  • 157
0

EDIT

   $patterns = '/\b[a-zA-Z]*\b/';

and use preg_match_all to match all occurences

  preg_match_all($patterns, $string, $result);

  $string = "Hallo dies ist ein extrem langer Text den ich gern gekuerz haette, leider bin ich selbst zu doof daf&uuml;r und mach hier einen test.";

 preg_match_all($patterns, $string, $result);

 foreach($result[0] as $value){
  echo "$value ";
 } 



for($length = count($result[0]), $i = 1; $i < 4; $i++){
       $offset = $length - $i;
       echo $result[0][$offset];
     }

the above snippet extracts the last 3 words

palerdot
  • 7,416
  • 5
  • 41
  • 47
  • sorry using this pattern gives this: The last word is:------ st. --------- thats the last three letters ... – hamburger Mar 08 '13 at 16:59
  • sorry . . .I misunderstood the question . . . . you can use \b to match word boundaries and retrieve any number of words from the resulting array – palerdot Mar 08 '13 at 17:25
  • this gives only the first word back .. and do not match all the words in the line. – hamburger Mar 08 '13 at 17:34
-1

It's probably better off using explode on the string, something like this:

<?php 
$cate = 'category-node-25';
$cateId = explode('-', $cate);
echo end($cateId);
?>

Output --> 25.

Magento2 Devloper
  • 119
  • 1
  • 1
  • 10
-1

Use preg_match to look for a word defined by the standard word-boundaries (as opposed to just spaces).

if (preg_match('/\w+\s*$/', $string, $matches)) {
    $last_word = $matches[0];
    echo $last_word;
} else {
    echo "No match found";
}
user1432181
  • 918
  • 1
  • 9
  • 24