0

I have the following HTML code

<b>Description: </b>TEXT TO BE GRABBED<br>

I need to use preg_match function to grab the "TEXT TO BE GRABBED".

I have tried the following without success:

<?php preg_match('#<b>Description: </b>(.*)<br>#', $content, $match); ?>

Can someone explain to me the proper method of extracting the text from the HTML?

Thank you.

user1985705
  • 43
  • 1
  • 4

2 Answers2

1

Don't parse HTML with regex. If you have to do so, then your attempt (in the original question before you edited it) was almost right, but you have an extra / in </br> -- there isn't one in your HTML. You should have:

preg_match('#<b>Description: </b>(.*)<br>#', $content, $match);
// $match[1] contains the text "TEXT TO BE GRABBED"

Obligatory link to explain why parsing HTML with regex is bad, very very bad...

Community
  • 1
  • 1
elixenide
  • 44,308
  • 16
  • 74
  • 100
0

The code you have included in the question works well :

<?php
$content = "<b>Description: </b>TEXT TO BE GRABBED<br>";
preg_match('#<b>Description: </b>(.*)<br>#', $content, $match);
print_r($match);

DEMO. And when I echo $match[1], I get the value :

TEXT TO BE GRABBED

Subin
  • 3,445
  • 1
  • 34
  • 63