I need a little regex to help me extract content from a div..
<div id="divname">
</div>
Within this div is the data I need.
PS Could you include the php code too?
Like preg_match ...
Thanks!
Moderator: General Moderators
Eww. That's definitely not what explode should be used for. That would create two arrays, and do so very strangely. This should definitely be done with regex.miro_igov wrote:You need to read this: http://php.net/manual/en/function.explode.php
Explode the content with separator your <div id="something"> then explode again element 1 in the result of the first explode to </div>.
This only works if you don't have any </div> inside the data you need.
Thnks for the tip! I'm almost there!miro_igov wrote:You need to read this: http://php.net/manual/en/function.explode.php
Explode the content with separator your <div id="something"> then explode again element 1 in the result of the first explode to </div>.
This only works if you don't have any </div> inside the data you need.
You are wrong, if you put /<div>(.*)<\/div>/ you will get the content between the first <div> and the last</div>superdezign wrote:Eww. That's definitely not what explode should be used for. That would create two arrays, and do so very strangely. This should definitely be done with regex.miro_igov wrote:You need to read this: http://php.net/manual/en/function.explode.php
Explode the content with separator your <div id="something"> then explode again element 1 in the result of the first explode to </div>.
This only works if you don't have any </div> inside the data you need.
If you know the start and end of what you want, just add a ".*" to the middle of those in the regex and viola. It's simple.
What is in the dots style="....." , if you really use these dots to detect dots you need to escape them with \. . The above example will replace everything from the first <p style="....."> to the last </p>, but if ht content in <p> does not contain html tags you may use /<p style=".....">[^<]*<\/p>/ivanfx wrote:Thnks for the tip! I'm almost there!miro_igov wrote:You need to read this: http://php.net/manual/en/function.explode.php
Explode the content with separator your <div id="something"> then explode again element 1 in the result of the first explode to </div>.
This only works if you don't have any </div> inside the data you need.
I just need an advice on how preg_replace works?
I need to remove two elements. One is a <p style=".....">Some content here</p> and
the other one is the <div id="footer"></a>. The </a> element was left during explode..
Anyway, I'm trying
$result = preg_replace('/<p style=".....">.*<\/p>/', ' ', $content);
but it's still there
miro_igov wrote:You are wrong, if you put /<div>(.*)<\/div>/ you will get the content between the first <div> and the last</div>superdezign wrote:Eww. That's definitely not what explode should be used for. That would create two arrays, and do so very strangely. This should definitely be done with regex.miro_igov wrote:You need to read this: http://php.net/manual/en/function.explode.php
Explode the content with separator your <div id="something"> then explode again element 1 in the result of the first explode to </div>.
This only works if you don't have any </div> inside the data you need.
If you know the start and end of what you want, just add a ".*" to the middle of those in the regex and viola. It's simple.
Code: Select all
preg_match('#<div id="foo">(.*?)</div>#', $data, $match);superdezign wrote:miro_igov wrote:You are wrong, if you put /<div>(.*)<\/div>/ you will get the content between the first <div> and the last</div>superdezign wrote: Eww. That's definitely not what explode should be used for. That would create two arrays, and do so very strangely. This should definitely be done with regex.
If you know the start and end of what you want, just add a ".*" to the middle of those in the regex and viola. It's simple.
I said "If you know the start and end of what you want," not "use <div>(.*)</div>." And I stand by not using explode for this. Explode is for generating arrays, not finding data.
Code: Select all
preg_match('#<div id="foo">(.*?)</div>#', $data, $match);
Code: Select all
<div id="foo"> Hello Wordl <p>Some info here</p><div id="bar">Ouch this is bad div</div> </div> And some other html here bla bla <div>another bad div, ouch ouch</div>