Posted By

Gustavs on 05/25/09


Tagged

regex php regular Expression


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

vali29
brianf


Regular Expression to get contents of div[id]


 / Published in: PHP
 

This allows you to get contents of any div with ID tag. $matches[0] returns with parent div tag $matches[1] returns with out parent div tag

  1. preg_match('/\<div id\=[\"]{0,1}ad_content[\"]{0,1}\>(.*?)\<\/div\>/s', $content['content_body'], $matches);
  2. $content['content_body'] = $matches[1];

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: djahren on October 4, 2010

This is really awesome and super helpful as I am still blindly making my way around regular expressions. There is a bit of a conundrum for me though, and I'm trying to think of a way around it myself but not getting very far. The issues is this: What if you have a div tag with other divs inside it. Preg_match will stop searching as soon as it hits the first closing div tag. Any thoughts?

You need to login to post a comment.