frosty
frosty

Reputation: 2649

Using word boundary instead of spaces in regex

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

Answers (2)

Mariano
Mariano

Reputation: 6511

If you want to remove the first and last word in a sentence, you can:

  1. explode() on spaces
  2. remove the first and last element with array_slice()
  3. implode() back again

Code

$inputX = "she hates my guts";
$result = implode(" ", array_slice(explode(" ", $inputX), 1, -1));

var_dump($result);

Output

string(8) "hates my"

Upvotes: 0

Lucas Trzesniewski
Lucas Trzesniewski

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

Related Questions