Jump to content

Recommended Posts

Hello,

 

I'm trying to extract a certain part of a string but having some issues.

 

Regex Code:

preg_match_all("/\<div class='toggler'\>([\w\d]+)\<\/div\>.*(\<div class='modelPickerItem'\>.*\<\/ul\>\<\/div\>(?:\<div class='cleanup'\>(?:\<\/div\>)+)+(?:\<div class='toggler'\>)?)+/U", $nolines, $results);

 

String tested using:

$nolines = "<div class='toggler'>Berlingo</div><div class=\"element\"><div><h3 class=\"modelDate\">1996 on</h3><div style=\"_height:1px;\"><div class='modelPickerItem'><a class='modelName' href=\"/vans/reviews/citroen/berlingo-2002.aspx\"> 2002 on </a><div style=\"float:left; margin-right: 10px\"><a href=\"/vans/reviews/citroen/berlingo-2002.aspx\"><img class=\"modelListImage\" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo_comm/IMG_3885.JPG' /></a></div><div class=\"modelDetails\"><ul class='forAgainst'><li class='plus'>Perhaps the best all-round small van</li><li class='minus'>Not as clever as newer rivals</li></ul></div><div class='cleanup'></div></div><div class='cleanup'></div><div class='modelPickerItem'><a class='modelName' href=\"/vans/reviews/citroen/berlingo-2008.aspx\"> 2008 on </a><div style=\"float:left; margin-right: 10px\"><a href=\"/vans/reviews/citroen/berlingo-2008.aspx\"><img class=\"modelListImage\" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo (08-)/2953.jpg' /></a></div><div class=\"modelDetails\"><ul class='forAgainst'><li class='plus'>All-round improvements over previous van, versatile features</li><li class='minus'>Lower power versions feel sluggish.</li></ul></div><div class='cleanup'></div></div><div class='cleanup'></div></div><div class='cleanup'></div></div></div><div class='toggler'>";

 

Expected result:

array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(642) "<div class='toggler'>Berlingo</div><div class="element"><div><h3 class="modelDate">1996 on</h3><div style="_height:1px;"><div class='modelPickerItem'><a class='modelName' href="/vans/reviews/citroen/berlingo-2002.aspx"> 2002 on </a><div style="float:left; margin-right: 10px"><a href="/vans/reviews/citroen/berlingo-2002.aspx"><img class="modelListImage" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo_comm/IMG_3885.JPG' /></a></div><div class="modelDetails"><ul class='forAgainst'><li class='plus'>Perhaps the best all-round small van</li><li class='minus'>Not as clever as newer rivals</li></ul></div><div class='cleanup'></div>"
  }
  [1]=>
  array(1) {
    [0]=>
    string( "Berlingo"
  }
  [2]=>
  array(1) {
    [0]=>
    string(521) "<div class='modelPickerItem'><a class='modelName' href="/vans/reviews/citroen/berlingo-2002.aspx"> 2002 on </a><div style="float:left; margin-right: 10px"><a href="/vans/reviews/citroen/berlingo-2002.aspx"><img class="modelListImage" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo_comm/IMG_3885.JPG' /></a></div><div class="modelDetails"><ul class='forAgainst'><li class='plus'>Perhaps the best all-round small van</li><li class='minus'>Not as clever as newer rivals</li></ul></div><div class='cleanup'></div></div><div class='cleanup'></div>"
    [1]=>
    string(647) "<div class='modelPickerItem'><a class='modelName' href="/vans/reviews/citroen/berlingo-2008.aspx"> 2008 on </a><div style="float:left; margin-right: 10px"><a href="/vans/reviews/citroen/berlingo-2008.aspx"><img class="modelListImage" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo (08-)/2953.jpg' /></a></div><div class="modelDetails"><ul class='forAgainst'><li class='plus'>All-round improvements over previous van, versatile features</li><li class='minus'>Lower power versions feel sluggish.</li></ul></div><div class='cleanup'></div></div><div class='cleanup'></div></div><div class='cleanup'></div></div></div><div class='toggler'>"
  }
}

 

Actual result:

array(3) {
  [0]=>
  array(1) {
    [0]=>
    string(642) "<div class='toggler'>Berlingo</div><div class="element"><div><h3 class="modelDate">1996 on</h3><div style="_height:1px;"><div class='modelPickerItem'><a class='modelName' href="/vans/reviews/citroen/berlingo-2002.aspx"> 2002 on </a><div style="float:left; margin-right: 10px"><a href="/vans/reviews/citroen/berlingo-2002.aspx"><img class="modelListImage" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo_comm/IMG_3885.JPG' /></a></div><div class="modelDetails"><ul class='forAgainst'><li class='plus'>Perhaps the best all-round small van</li><li class='minus'>Not as clever as newer rivals</li></ul></div><div class='cleanup'></div>"
  }
  [1]=>
  array(1) {
    [0]=>
    string( "Berlingo"
  }
  [2]=>
  array(1) {
    [0]=>
    string(521) "<div class='modelPickerItem'><a class='modelName' href="/vans/reviews/citroen/berlingo-2002.aspx"> 2002 on </a><div style="float:left; margin-right: 10px"><a href="/vans/reviews/citroen/berlingo-2002.aspx"><img class="modelListImage" src='/imagecache/file/160x100/archive/LCV/Citroen/Berlingo_comm/IMG_3885.JPG' /></a></div><div class="modelDetails"><ul class='forAgainst'><li class='plus'>Perhaps the best all-round small van</li><li class='minus'>Not as clever as newer rivals</li></ul></div><div class='cleanup'></div>"
  }
}

 

Thanks In advance.

Link to comment
https://forums.phpfreaks.com/topic/144067-regex-help/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.