Hello everyone,

I have a small problem for using a regex in PHP. The problem is that I get a text output from another website. I want to get some informations by using a regex but I'm not sure in advance how the informations will be display.
Here the example:
Text Output

<br/><b>Description:</b><br/>This is a good description<br/><b>Title:</b><br/>MY TITLE<br/><b>Comment:</b><br/>Very interesting<br />...

I would like to get only the content of Description (without the label too) but I'm not sure that all the other label will be there and in this way, so it could be only title or only comment or any of them.
The only way I found is if I am sure both of them are present and fix:

$pattern = '#<b>Description</b><br/>(.+)<br/><b>Title</b><br/>(.+)<br/><b>Comment</b><br/>(.+)#';
if (preg_match($pattern, $description, $match)) {
  $description = $match[1];  // Text description

How can I do?

Thank you.

6 Years
Discussion Span
Last Post by pritaeas
$pattern = '#<b>Description:</b><br/>(.+?)<br/>(<b>Title:</b><br/>(.+?)<br/>)?(<b>Comment:</b><br/>(.+?))?#';

I've made the title and comment optional as you can see.

If you don't need the title and comment, you can just omit that whole part.

Edited by pritaeas: n/a


Hi, thank you for your answer.
It works perfectly in most of the cases. However, if there is a break in the description, it won't work? Why? I thought if you are using .+ it will take all characters!
And how can I change to get also the one with <br> or <br /> instead of <br/>? I thought something like r[/\s], but how can I get the ones with nothing after the r?

Thank you again.


How about this ?

$pattern = '#<b>Description:</b>(.+?)(<b>Title:</b>(.+?))?(<b>Comment:</b>(.+?))?#';

Although now the br tags are in your result, you can easily replace them with

$description = preg_replace('/<br\s*/?>/i', '', $description);
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.