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?

Member Avatar
diafol

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

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

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.

Thank you for explanation guys. Greatly appreciated.