I have an html page that I loaded into a variable ($page) via cURL, and I need to extract a piece of information from it. There is a tag such as: <h2 class="yxz">MY TEXT</h2> And I want to extract the 'my text' inside the tag. There is only going to be one of such tags on the page, so I used strpos to get the location of the opening tag, and then str_replace to cut everything out before the tag. But the amount of text inside the tag is a variable length, and I am having trouble extracting it. I now have a string ($str) that looks like: SOME TEXT HERE </h2> .... I tried using the regex: /[^<]+/ (greedily select any number of characters that isn't a '<' ) with preg_split to get everything before the </h2>, but I can't seem to get it working. Thanks. And I realize that this can be done without a regular expression, but I need to learn how to work regex within php.
Use this pattern to find any <h2> tag <?php $pattern = "(<h2[^>]*?>([^<]*?)</h2>)"; $valid = preg_match($pattern, $data, $SEARCH); if ($valid) { echo $SEARCH[1]; } ?> PHP: .. and this pattern for specific h2.class name. (eg. yxz) <?php $pattern = "(<h2[^>]*?class=\"yxz\">([^<]*?)</h2>)"; $valid = preg_match($pattern, $data, $SEARCH); if ($valid) { echo $SEARCH[1]; } ?> PHP: