0

So I haven't used preg_match too terribly often but I found this example online for parsing a page to get text from div's:

preg_match("/<div class=\"topic\">(.*?)<div class=\"content\">(.*?)<\/div>/", $html, $matches);

Now everything looks familiar to me and I understand until I see the (.*?) What role does it play in preg_match?

Edited by Bensirpent07

3
Contributors
3
Replies
10
Views
1 Year
Discussion Span
Last Post by Bensirpent07
1

. = any character - * = any number of - ? = optional

You may fare better with DOMDocument or SImpleXML. preg_matching on HTML tags can be awkward.

1

There is a distinction here:

.* means take as many characters you can before moving on to the next part.

.*? means take as few characters you can before moving on to the next part.

The difference is easily checked when there are many closing divs within your source string.

This question has already been answered. 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.