Remove last two words from a string
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);
Comments
-
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 about 12 yearsThanks 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 about 12 yearsThanks 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 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 about 12 yearsThanks 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 about 12 years@Andrej Provide me a codepad link to your setup and I'll let you know.
-
r0skar about 12 yearsI 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 about 12 years@Andrej What is the
echo implode( " ", $words );
line outputting? -
Sampson about 12 yearsCan you do
var_dump( $descr->plaintext );
and share the output? -
r0skar about 12 yearsI am probably being very stupid now, but in the example you made, I still see
Source: CBS
in the "Output" box? -
Sampson about 12 yearsI 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 about 12 yearsOh 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!