Reputation: 2649
I'm trying to use regex to get rid of the first and last word in a sentence, but it only works when I'm using spaces to match, but it doesn't work when I'm trying to use word boundary. Why is that?
Using Spaces:
$inputX = "she hates my guts";
preg_match("~ .+ ~i", $inputX, $match);
print_r($match);
Result:
Array ( [0] => hates my )
Using Word Boundary:
$inputX = "she hates my guts";
preg_match("~\b.+\b~i", $inputX, $match);
print_r($match);
Result:
Array ( [0] => she hates my guts )
Upvotes: 0
Views: 57
Reputation: 6511
If you want to remove the first and last word in a sentence, you can:
explode()
on spacesarray_slice()
implode()
back againCode
$inputX = "she hates my guts";
$result = implode(" ", array_slice(explode(" ", $inputX), 1, -1));
var_dump($result);
Output
string(8) "hates my"
Upvotes: 0
Reputation: 51330
Here are the word boundaries:
s h e h a t e s m y g u t s
^ ^ ^ ^ ^ ^ ^ ^
So your pattern matches like this:
s h e h a t e s m y g u t s
^\_______________________________/^
| | |
\b .+ \b
If you want to get rid of the first and last word, I'd just replace these with an empty string, using the following pattern:
^\W*?\w+\s*|\s*\w+\W*$
Both \W*
are there to account for possible punctuation (ie she hates my guts.
) but you can remove them if they're not needed.
Upvotes: 1