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?

Recommended Answers

All 3 Replies

Member Avatar for 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.

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.