-1

hi all my sample code is below :

  <?php 
  $html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>samplecode</title>
  </head>
  <body>
    <div id="warrper">
      <div class="box-title">This title is sample</div>
      <div class="box-maim">
        <div class="box-element-1">
           <ul>
              <li>sample 1</li>
              <li>sample 2</li>
              <li>sample 3</li>
              <li>sample 4</li>
              <li>sample 5</li>
           </ul>
        </div>
        <div class="box-element-1">
           <ul>
              <li>sample 1</li>
              <li>sample 2</li>
              <li>sample 3</li>
              <li>sample 4</li>
              <li>sample 5</li>
           </ul>   
        </div>
        <div class="box-element-1">
           <ul>
              <li>sample 1</li>
              <li>sample 2</li>
              <li>sample 3</li>
              <li>sample 4</li>
              <li>sample 5</li>
           </ul>
        </div>
      </div>
    </div>
  </body>
  </html> ';

   preg_match( '/<div class="box-maim">(.*?)<\/div>/si' , $html , $match );

   print_r($match);
  ?>

my aim after load html from url is , fetch just only tag part of selected class for example below code :

  <div class="box-element-1">
     <ul>
        <li>sample 1</li>
        <li>sample 2</li>
        <li>sample 3</li>
        <li>sample 4</li>
        <li>sample 5</li>
     </ul>
  </div>
  <div class="box-element-1">
     <ul>
        <li>sample 1</li>
        <li>sample 2</li>
        <li>sample 3</li>
        <li>sample 4</li>
        <li>sample 5</li>
     </ul>   
  </div>
  <div class="box-element-1">
     <ul>
        <li>sample 1</li>
        <li>sample 2</li>
        <li>sample 3</li>
        <li>sample 4</li>
        <li>sample 5</li>
     </ul>
  </div>

but i dont know the correct methods of this action for that part .

  • You can do it with javascript or jQuery or you can use PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/) for PHP. – Konsole Sep 27 '13 at 05:20
  • This regex will stop at first – Hanky Panky Sep 27 '13 at 05:21
  • i have to fetch data server side , do not client side – rezataslimi_edu Sep 27 '13 at 05:26
  • @Console I am curious why do you suggest simplehtmldom if you have DomDocument and SimpleXML available by default on most installations of PHP this days. Is there a particular reason or just preference? – Prix Sep 27 '13 at 05:26
  • i don't know how use javascript or jquery server side ?! – rezataslimi_edu Sep 27 '13 at 05:27
  • thanks for help me , but i don't know how use DOMXPath – rezataslimi_edu Sep 27 '13 at 05:30
  • 1
    Then read the link above it shows how to use it and also have 3 links showing how to use it for specific things. There are very few and specific cases where you would like to use regex to parse HTML and believe me, yours isn't one of it. – Prix Sep 27 '13 at 05:31
  • can you show this sample how fetch with DOMXpath ? – rezataslimi_edu Sep 27 '13 at 05:33
  • A basic usage example can be found in [Grabbing the href attribute of an A element](http://stackoverflow.com/questions/3820666/regular-expression-for-grabbing-the-href-attribute-of-an-a-element/3820783#3820783) and a general conceptual overview can be found at [Noob question about DOMDocument in php](http://stackoverflow.com/questions/4979836/noob-question-about-domdocument-in-php/4983721#4983721). – Prix Sep 27 '13 at 05:35
  • @rezataslimi_edu There's plenty of help already out there, including documentation. Just search for it. – BLaZuRE Sep 27 '13 at 05:35
  • [How to use the DOM extension has been covered extensively on StackOverflow](http://stackoverflow.com/search?q=DOM+HTML+%5bPHP%5d&submit=search), so if you choose to use it, you can be sure most of the issues you run into can be solved by searching/browsing Stack Overflow. – Prix Sep 27 '13 at 05:35

2 Answers2

-1

As all suggested to use DOM, try the below code:

<?php
$html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  <html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>samplecode</title>
  </head>
  <body>
    <div id="warrper">
      <div class="box-title">This title is sample</div>
      <div class="box-maim">
        <div class="box-element-1">
           <ul>
              <li>sample 1</li>
              <li>sample 2</li>
              <li>sample 3</li>
              <li>sample 4</li>
              <li>sample 5</li>
           </ul>
        </div>
        <div class="box-element-1">
           <ul>
              <li>sample 1</li>
              <li>sample 2</li>
              <li>sample 3</li>
              <li>sample 4</li>
              <li>sample 5</li>
           </ul>   
        </div>
        <div class="box-element-1">
           <ul>
              <li>sample 1</li>
              <li>sample 2</li>
              <li>sample 3</li>
              <li>sample 4</li>
              <li>sample 5</li>
           </ul>
        </div>
      </div>
    </div>
  </body>
  </html> ';

$dom = new DOMDocument();    
$dom->loadHTML($html);    
$xpath = new DOMXPath($dom);    
$div = $xpath->query('//div[@class="box-maim"]');    
$div = $div->item(0);    
echo $dom->saveXML($div);    
?>

It works perfectly :)

Prix
  • 19,417
  • 15
  • 73
  • 132
Vishnu
  • 2,372
  • 6
  • 36
  • 58
-1

If I am getting you properly, just do simply like that:

preg_match_all('/<div\s[^>]*class=\"box-element-([^\"]*)\"[^>]*>(.*)<\/div>/siU', $html, $matches, PREG_SET_ORDER);
echo '<pre>';
print_r($matches);
tckmn
  • 57,719
  • 27
  • 114
  • 156
Satyam Saxena
  • 581
  • 2
  • 10