Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

REGEX - PHP Get only bold section in a string

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

like image 705
Ahmar Ali Avatar asked May 09 '26 00:05

Ahmar Ali


2 Answers

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. A DOM parser is more stable and convenient for that purpose.

Example code:

$doc = new DOMDocument();
$doc->loadHTML('Hello <b>ABCD</b> World');

foreach($doc->getElementsByTagName('b') as $element) {
    echo $element->nodeValue;
}
like image 137
hek2mgl Avatar answered May 10 '26 14:05

hek2mgl


use preg_match_all:

preg_match_all("'<b>(.*?)</b>'si", $text, $match);

foreach($match[1] as $val)
{
    echo $val."<br>";
}
like image 20
num8er Avatar answered May 10 '26 12:05

num8er



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!