repeating html:

<div class="container" style="position:relative;">
....
....
....
</div>
<div class="container" style="position:relative;">
....
....
....
</div>

im trying to get each of these into their own array object using preg_match:

$file = file_get_contents('file.html');

$search = '/\<div class="container" style="position:relative;"\>(.*)\<\/div\>\<div class="container" style="position:relative;"\>/s';

preg_match($search,$file,$matches);

echo count($matches);
echo '<pre>';
print_r($matches);
echo '</pre>';

and its just not working... I've had mixed results toying with the regex but the best i get is one result with everything in it

yea im horrid with regexs 🙁

    man... i guess its not just me who sucks at regex's tonight... laserlight must be on vaca

      NogDog;10900876 wrote:

      preg_match_all(), perhaps?

      would it be easier to use the DOM functions or perhaps use the DOM features of phpQuery... With phpQuery I'm told I could load the html and then use

      pq('div.container')
      

      ...to parse it

      If it's just as easily done in the native DOM I wouldn't mind that but I've never used it so I'd need some code to get me going in the right direction

        this actually seems to work OK

        $doc = new DOMDocument();
        $doc->loadHTMLFile('doc.htm');
        
        foreach ($doc->getElementsByTagName('div') as $nodes)
        {
        	if ($nodes->getAttribute('class') == 'container')
        	{
        		echo preg_replace('/[\n\r\t\v\f][\s]{2,}/',' ',trim($nodes->nodeValue))."\n";
        	}
        }
        

        my only question is, how can i check elements within my getElementsByTagName('div') nodes to see if a anchor tag within has a class value of 'foobar' and if so, dont include it in $nodes

          scrupul0us wrote:

          my only question is, how can i check elements within my getElementsByTagName('div') nodes to see if a anchor tag within has a class value of 'foobar' and if so, dont include it in $nodes

          Ah, now there you can have a look at using XPath.

          //div[@class='container'][not(.//a[@class='foo'])]
          

          (On a technical note: since the "class" attribute is interpreted as a space-separated list, if you use multiple classes on one element you'd need to replace the plain equality tests with XPath string search functions).

            Weedpacket;10900948 wrote:

            Ah, now there you can have a look at using XPath.

            //div[@class='container'][not(.//a[@class='foo'])]
            

            (On a technical note: since the "class" attribute is interpreted as a space-separated list, if you use multiple classes on one element you'd need to replace the plain equality tests with XPath string search functions).

            ive used it with simpleXML so it shouldnt be too bad to use here... thanks weed

              i actually found a decent way todo this without xpath... i just need to combine two arrays such that the values of the second array are appended to the end of the first... not like array_merge()

              eg:

              $array1 = array('a','b','c');
              $array2 = array('1','2','3');
              
              $resultant = array('a1','b2','c3');
              

              is there a way todo that without using a for loop? I didnt see a native array function to do this

                Write a Reply...