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
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:
The expression broken down follows:
Demo: http://codepad.org/G22LnDDY
One other method would be to use
explodeto create an array of words, and remove the last two.Demo: http://codepad.org/6XwqvwuP