I am new to Regex. I have a string like:
Hello <b>ABCD</b> World
or
<b>ABCD</b>Hello World
I basically want to retain the text inside bold tags but remove all other characters in the string.
I have found the code to remove bold part in the string:
$string = 'This is <b>an</b> example <b>text</b>';
echo preg_replace('/(<b>.+?)+(<\/b>)/i', '', $string);
So how do I make it to work in opposite way?
Regards Ahmar
use preg_match_all:
Use a
DOM
parser instead of a regex if you want to extract data from a HTML or XML document. While a regex will work in simple cases too, it can get weird if the use case gets more complicated or the input data changes in an unexpected way. ADOM
parser is more stable and convenient for that purpose.Example code:
Try this