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.

$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.

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);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.