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 )
.is greedy so.+?but then the result isshebecause start of string is a boundary.\bis beforesheand aftergutsas well.preg_match_all()so onlyshe.\bwill also match positions in the first and last word