Remove last two words from a string

10,219

Solution 1

A simple regular expression could match a space, followed by any number of letters (or a colon), followed by a space, followed by any number of letters at the end of a string:

$str = "Lorem ipsum dolor sit amet. Source: LOREM";
$str = preg_replace( "/\s[a-z:]+\s[a-z]+$/i", "", $str );

// Lorem ipsum dolor sit amet.
echo $str;

The expression broken down follows:

\s       // Single space
[a-z:]+  // Any letter, a to z, or a colon, 1 or more times
\s       // Single space
[a-z]+   // Any letter, a to z, 1 or more times
$        // End of string

Demo: http://codepad.org/G22LnDDY

One other method would be to use explode to create an array of words, and remove the last two.

$str = "Lorem ipsum dolor sit amet. Source: LOREM";
$words = explode( " ", $str );
array_splice( $words, -2 );

// Lorem ipsum dolor sit amet.
echo implode( " ", $words );

Demo: http://codepad.org/6XwqvwuP

Solution 2

$str = ' would like to remove Source: LOREM';
$str =  preg_replace('/(.*)\s+([a-zA-Z]+):\s+([a-zA-Z]+)$/', '$1', $str);
echo $str;

Solution 3

You don't have to use regexp here. Assuming you define a word as anything surrounded by a space:

$words = explode(' ', 'Lorem ipsum dolor sit amet. Source: LOREM');
if (count($words) >= 2) {
    array_pop($words);
    array_pop($words);
}
$words = implode(' ', $words);

Or:

$words = explode(' ', 'Lorem ipsum dolor sit amet. Source: LOREM');
if (count($words) >= 2) {
    $words = array_slice($words, 0, count($words) - 2);
}
$words = implode(' ', $words);
Share:
10,219
r0skar
Author by

r0skar

meh

Updated on June 04, 2022

Comments

  • r0skar
    r0skar almost 2 years

    I am totally new to preg_replace, but the code below removes the last word from my string:

    preg_replace('/\W\w+\s*(\W*)$/', '$1', $var);
    

    I am trying to modify it, so that it removes the last two words.

    The only thing that I could think of was to replace $1 with $2, but this seems to not have any effect at all and was probably just dumb :/

    The string in question looks kinda like this:

    Lorem ipsum dolor sit amet. Source: LOREM
    

    I would like to remove Source: LOREM

  • r0skar
    r0skar about 12 years
    Thanks for all the answers, but none seems to work (Source: Lorem is not removed). Its strange, since that code to remove only 1 word works fine. Its probably a problem related to my code. I will keep trying it and if I dont get it fixed, I will update my question with the code snippet. I hope its fine if i dont accept any answer as of now.
  • r0skar
    r0skar about 12 years
    Thanks for all the answers, but none seems to work (Source: Lorem is not removed). Its strange, since that code to remove only 1 word works fine. Its probably a problem related to my code. I will keep trying it and if I dont get it fixed, I will update my question with the code snippet. I hope its fine if i dont accept any answer as of now.
  • Sampson
    Sampson about 12 years
    @Andrej Both methods above work. Check your code again. I've provided links to two functional demos that you can play with.
  • r0skar
    r0skar about 12 years
    Thanks a lot for your effort, Jonathan. Codepad.org looks like a great way to test my whole snippet! I am sure I will get it to work sooner or later! p.s. could the problem be that I have multiple strings and I do use the expression in a for each loop?
  • Sampson
    Sampson about 12 years
    @Andrej Provide me a codepad link to your setup and I'll let you know.
  • r0skar
    r0skar about 12 years
    I put it online at codepad.org/9ShnX8xP . However the whole code doesnt work there, since a file (simplehtmldomparser) is missing. If you need me to include that one too, just let me know! And thanks again for taking your time to look at it!
  • Sampson
    Sampson about 12 years
    @Andrej What is the echo implode( " ", $words ); line outputting?
  • Sampson
    Sampson about 12 years
    Can you do var_dump( $descr->plaintext ); and share the output?
  • r0skar
    r0skar about 12 years
    I am probably being very stupid now, but in the example you made, I still see Source: CBS in the "Output" box?
  • Sampson
    Sampson about 12 years
    I think you're looking in the wrong place: i.imgur.com/hsbBu.png Now how the output doesn't have multiple new lines like the input did.
  • r0skar
    r0skar about 12 years
    Oh I get it! So I would take that output now and use it for one of the various methods that have been posted here? Trying -> WORKS! :) Thanks alot for your efforts! I would double upvote your answer if I could!