PHP Regular Expression Exercises: Remove the last word from a string
PHP regular expression: Exercise-2 with Solution
Write a PHP script that removes the last word from a string.
Sample string : 'The quick brown fox'
<?php $str1 = 'The quick brown fox'; echo preg_replace('/\W\w+\s*(\W*)$/', '$1', $str1)."\n"; ?>
The quick brown
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?