kusanagi
kusanagi

Reputation: 14614

php regex strange error

i use such regex $msg = preg_replace('/<b>(\w)<\/b>/', '9999', $msg); to replace <b>test</b> but it not replace. why?

Upvotes: 1

Views: 68

Answers (4)

Elijan
Elijan

Reputation: 1416

Notice the plus after \w+

 $msg = preg_replace('/<b>(\w+)<\/b>/', '9999', $msg);

Upvotes: 0

Gaurav
Gaurav

Reputation: 28755

try this

$msg = preg_replace('#<b>(\w)*<\/b>#', '9999', $msg);

Upvotes: 0

Stephan B
Stephan B

Reputation: 3701

Your \w does not match. I don't find my regex manual right now, but use something like .*.

Upvotes: 0

Kaivosukeltaja
Kaivosukeltaja

Reputation: 15735

You're missing the quantity token. That would only match one character long strings between the <b> tags.

$msg = preg_replace('/<b>(\w*)<\/b>/', '9999', $msg); 

Upvotes: 5

Related Questions