regex - How to parse <a name and <image src= inside <li tag using php? -


i got html string lots of <li> .. </li> sets. want parse following data each set of <li> ...</li> :

   1: call.php?category=fruits&amp;fruitid=123456    2: mango season    3: http://imagehosting.com/images/fru_123456.png 

i used preg_match_all first value how second , third value ? happy if show me second , third item .thanks in advance.

php:

preg_match_all('/getit(.*?)detailfruit/', $code2, $match);  var_dump($match);    // iterate new array   for($i = 0; $i < count($match[0]); $i++) { $code3=str_replace('getit(\'', '', $match[0]); $code4=str_replace('&amp;\',detailfruit', '', $code3); echo "<br>".$code4[$i]; } 

sample <li> ..</li> data:

<li><a id="fr123456" onclick="setfood(false);setseasonfruitid('123456');getit('call.php?category=fruits&amp;fruitid=123456&amp;',detailfruit,false);">mango season</a><img src="http://imagehosting.com/images/fru_123456.png">             </li> 

edit: used dom got 2 , 3 value how first value using dom ?

libxml_use_internal_errors(true); $dom = new domdocument; $dom->loadhtml($code2); $xpath = new domxpath($dom);  // empty array hold links return $result = array();  //loop through each <li> tag in dom foreach($dom->getelementsbytagname('li') $li) {     //loop through each <a> tag within li, extract node value     foreach($li->getelementsbytagname('a') $links){         $result[] = $links->nodevalue;         echo $result[0] . "\n";     }      $imgs = $xpath->query("//li/img/@src");  foreach ($imgs $img) {     echo $img->nodevalue . "\n"; } } 

interesting question :-) following solution uses combination of domdocument/simplexml values 2 & 3 easily. domdocument used html snippet corrupted. link (value 1) javascript content, simple regex used:

~getit\('([^']+)'\)~ # search getit( , singlequote literally # capture (but not including) new single quote # saved in group 1 

a complete walkthrough can found below (obviously made banana part):

<?php $html = '<ul> <li><a id="fr123456" onclick="setfood(false);setseasonfruitid(\'123456\');getit(\'call.php?category=fruits&amp;fruitid=123456&amp;\',detailfruit,false);">mango season</a><img src="http://imagehosting.com/images/fru_123456.png"></li> <li><a id="fr7890" onclick="setfood(false);setseasonfruitid(\'7890\');getit(\'call.php?category=fruits&amp;fruitid=7890&amp;\',detailfruit,false);">bananas</a><img src="http://imagehosting.com/images/fru_7890.png"></li>         </ul>';  $dom = new domdocument; $dom->stricterrorchecking = false; $dom->loadhtml($html); $xml = simplexml_import_dom($dom);  # xpath find list items $items = $xml->xpath("//ul/li");  $regex = "~getit\('([^']+)'\)~";  # loop on items foreach ($items $item) {     $title = $item->a->__tostring();     $imglink = $item->img["src"];      $jslink = $item->a["onclick"];      preg_match_all($regex, $jslink, $matches);     $jslink = $matches[1][0];      echo "title: $title, imglink: $imglink, jslink: $jslink\n";     // output: title: mango season, imglink: http://imagehosting.com/images/fru_123456.png, jslink: call.php?category=fruits&fruitid=123456&     //         title: bananas, imglink: http://imagehosting.com/images/fru_7890.png, jslink: call.php?category=fruits&fruitid=7890& }  ?> 

Comments

Popular posts from this blog

c - How to retrieve a variable from the Apache configuration inside the module? -

c# - Constructor arguments cannot be passed for interface mocks -

python - malformed header from script index.py Bad header -