I'm using regex to match specific div's in a page and replace them with a custom formatted one. I can't use domdocument as often the pages we process are mal-formed and after running it through domdocument, the pages are reformatted and don't display the same.
I'm currently using the following which works perfectly:
preg_match('#(\<div id=[\'|"]'.$key.'[\'|"](.*?)\>)(.*?)\<\/div\>#s', $contents, $response);
To match div tags such as:
<div id="test"></div>
<div id="test" style="width: 300px; height: 200px;"></div>
etc...
The problem I'm encountering is tags where the id is after the style or class, example:
<div class="test" id="test"></div>
If I run the following, the regex seems to become greedy and matches a ton of html before the div tag, so I'm not sure how to fix this:
preg_match('#(\<div(.*?)id=[\'|"]'.$key.'[\'|"](.*?)\>)(.*?)\<\/div\>#s', $contents, $response);
Does anyone have any ideas?