Jump to content

Baffled- preg_match using wildcard - how to escape possible special chars


DamienRoche

Recommended Posts

I am having trouble understanding this.

 

Here is my code:

$html = "blah blah <strong>what.what = huh?</strong> blah blah";
preg_match('#<strong>what.what = huh?</strong>#', $html, $match);
echo "<br>match: $match[0]";

 

The bit inbetween the strong tags will be extracted using a wildcard, so how would I go about escaping special characters?

 

Any insight on this is welcomed. Thanks.

You spelt 'whatever' wrong in the regex. If you want to match any characters inside <strong> tags, try this:

 

<?php

$html = "blah blah <strong>whatever</strong> blah blah";
preg_match('#<strong>.*?</strong>#', $html, $match);
echo "<br>match: $match[0]";

?>

 

Oh and $match[1] would be the one you want to echo.

Sorry, I sussed that out. I had a few issues so I've edited the original post to reflect my real problem.

 

I have also tried $match[1]; but that didn't work.

 

Thanks.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.