Grabbing specific elements inside DIV from external page - php

I need to scrap the following elements inside each one of these div's class="product-grid-item" (page contains several of them), but in fact I have no clue how to do it... so, I need help not to pull my hair out.
1 - The link and image inside the div: class="product-element-top2;
<a href="https://...this_link" class="product-image-link"> (just need the link)
<img width="300" height="300" src="https://...this_image_url... (just need this image URL)
2 - The title inside the h3 tag as follows;
<h3 class="wd-entities-title"><a href="https://...linkhere">The title goes here (just the title)
3 - Last but not least, I need to grab tha price inside this;
<span class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">€</span>20,00</bdi></span></span> (just the "€20.00")
Here's the full HTML:
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://...linkhere" class="product-image-link">
<img width="300" height="300" src="https://image-goes-here.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail"> </a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title">The title goes here</h3>
<span class="price"><span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">€</span>20,00</bdi></span></span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<span>Options</span></div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
Buy
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
quick view
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://linkhere/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon">Close</div>
<div class="quick-shop-form wd-scroll-content">
</div>
</div>
</div>
</div>
One of my clumsy attempts:
$html = file_get_contents("https://url-here.goetohere");
$DOM = new DOMDocument();
$DOM->loadHTML($html);
$finder = new DomXPath($DOM);
$classname = 'product-grid-item';
$classname = 'product-element-top2';
$classname = 'product-element-top2';
$classname = 'wd-entities-title';
$classname = 'price';
$nodes = $finder->query("//*[contains(#class, '$classname')]");
foreach ($nodes as $node) {
echo 'here »» ' . htmlentities($node->nodeValue) . '<br>';
}

Assuming that the HTML is being fetched correctly prior to attempting any DOM processing then it is fairly straightforward to construct some basic XPath expressions to find the indicated content.
As per the comment page contains several of them there are 2 product-grid-item divs as you'll note in the output.
$html='
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://...linkhere" class="product-image-link">
<img width="300" height="300" src="https://image-goes-here.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail">
</a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title">
The title goes here
</h3>
<span class="price">
<span class="woocommerce-Price-amount amount">
<bdi>
<span class="woocommerce-Price-currencySymbol">€</span>20,00
</bdi>
</span>
</span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<a href="https://...linkhere" data-quantity="1" class="button product_type_variable add_to_cart_button add-to-cart-loop">
<span>Options</span>
</a>
</div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
Buy
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
quick view
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://linkhere/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon">
Close
</div>
<div class="quick-shop-form wd-scroll-content"></div>
</div>
</div>
</div>
<div class="product-grid-item" data-loop="1">
<div class="product-element-top">
<a href="https://www.example.com/banana" class="product-image-link">
<img width="300" height="300" src="https://www.example.com/kittykat.jpg" class="attachment-woocommerce_thumbnail size-woocommerce_thumbnail">
</a>
<div class="top-information wd-fill">
<h3 class="wd-entities-title">
Oh look, another title!
</h3>
<span class="price">
<span class="woocommerce-Price-amount amount">
<bdi>
<span class="woocommerce-Price-currencySymbol">€</span>540,00
</bdi>
</span>
</span>
<div class="wd-add-btn wd-add-btn-replace woodmart-add-btn">
<a href="https://www.example.com/gorilla" data-quantity="1" class="button product_type_variable add_to_cart_button add-to-cart-loop">
<span>Options</span>
</a>
</div>
</div>
<div class="wd-buttons wd-pos-r-t color-scheme-light woodmart-buttons">
<div class="wd-compare-btn product-compare-button wd-action-btn wd-style-icon wd-compare-icon">
Buy
</div>
<div class="quick-view wd-action-btn wd-style-icon wd-quick-view-icon wd-quick-view-btn">
quick view
</div>
<div class="wd-wishlist-btn wd-action-btn wd-style-icon wd-wishlist-icon woodmart-wishlist-btn">
<a class="" href="https://www.example.com/wishlist/" data-key="dcf36756534755" data-product-id="387654" data-added-text="See Wishlist">Wishlist</a>
</div>
</div>
<div class="quick-shop-wrapper wd-fill wd-scroll">
<div class="quick-shop-close wd-action-btn wd-style-text wd-cross-icon">
Close
</div>
<div class="quick-shop-form wd-scroll-content"></div>
</div>
</div>
</div>';
To process the downloaded HTML
# set the libxml parameters and create new DOMDocument/XPath objects.
libxml_use_internal_errors( true );
$dom=new DOMDocument;
$dom->validateOnParse=false;
$dom->strictErrorChecking=false;
$dom->recover=true;
$dom->loadHTML( $html );
libxml_clear_errors();
$xp=new DOMXPath( $dom );
# some basic XPath expressions
$exprs=(object)array(
'product-link' => '//a[#class="product-image-link"]',
'product-img-src' => '//a[#class="product-image-link"]/img',
'h3-title-text' => '//h3[#class="wd-entities-title"]',
'price' => '//span[#class="price"]/span/bdi'
);
# find the keys (for convenience) to be used below
$keys=array_keys( get_object_vars( $exprs ) );
# store results here
$res=array();
# loop through all patterns and issue XPath query.
foreach( $exprs as $key => $expr ){
# add key to output and set as an array.
$res[ $key ]=[];
$col=$xp->query( $expr );
# find the data if the query succeeds
if( $col && $col->length > 0 ){
foreach( $col as $node ){
switch( $key ){
case $keys[0]:$res[$key][]=$node->getAttribute('href');break;
case $keys[1]:$res[$key][]=$node->getAttribute('src');break;
case $keys[2]:$res[$key][]=trim($node->textContent);break;
case $keys[3]:$res[$key][]=trim($node->textContent);break;
}
}
}
}
# show the result or do really interesting things with the data
printf('<pre>%s</pre>',print_r($res,true));
Which yields:
Array
(
[product-link] => Array
(
[0] => https://...linkhere
[1] => https://www.example.com/banana
)
[product-img-src] => Array
(
[0] => https://image-goes-here.jpg
[1] => https://www.example.com/kittykat.jpg
)
[h3-title-text] => Array
(
[0] => The title goes here
[1] => Oh look, another title!
)
[price] => Array
(
[0] => â¬20,00
[1] => â¬540,00
)
)

Related

parse a html heterogeneous list in php using simple_html_dom

I want to parse a html list consist of article tag and I found simple_html_dom I read the doc but can't make it going well
I have pages like
<article class="post">
<div class="leftc"> <figure class="ico-2721"></figure>
<header>
<h2>
<a href="http://pop-music.ir/%d8%a2%d9%87%d9%86%da%af-%d8%ac%d8%af%db%8c%d8%af-%d9%85%db%8c%d9%84%d8%a7%d8%af-%d8%a8%d8%a7%d8%a8%d8%a7%db%8c%db%8c-%d8%a8%d9%86%d8%a7%d9%85-%db%8c%d8%a7%d8%aa%d9%88-%db%8c%d8%a7%d8%aa%d9%88"
rel="bookmark" title="دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو">دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو</a>
</h2>
<span class="cat">دسته بندی :
آهنگ شاد,
تک آهنگ
</span>
<span class="date">تاریخ : ۲۱ام اسفند, ۱۳۹۶ </span>
</header>
<p style="text-align: center;">
<img class="alignnone" title="دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو" src="http://dl.pop-music.ir/images/1396/Esfand/Milad-Babaei.jpg"
alt="دانلود آهنگ جدید میلاد بابایی بنام یاتو یاتو" width="500" height="500">
</p>
<p style="text-align: center;">
<a title="دانلود آهنگ جدید" href="http://pop-music.ir/" target="_blank" rel="noopener noreferrer">دانلود آهنگ جدید</a>
<strong>میلاد بابایی</strong> بنام
<strong>یاتو یاتو</strong> با بالاترین کیفیت</p>
<p style="text-align: center;">
Download New Music
Milad Babaei – Yato Yato</p>
<p style="text-align: center;">
<span style="color: #0000ff;">ترانه: عاطفه حبیبی , موزیک: میلاد بابایی , تنظیم: امیر ارشیا</span>
</p>
<div style="text-align: center;">
<div>
<p>برای
<a title="دانلود آهنگ" href="http://pop-music.ir/tag/%D8%AF%D8%A7%D9%86%D9%84%D9%88%D8%AF-%D8%A2%D9%87%D9%86%DA%AF"
target="_blank" rel="noopener noreferrer">دانلود آهنگ</a> به ادامه مطلب مراجعه کنید …</p>
</div>
</div>
<p style="text-align: center;">
</p>
<div class="morelink">
دانلود + ادامه مطلب
<div class="rating"></div>
</div>
<div class="pull-left"></div>
<div class="clear"></div>
</div>
<div class="clear"></div>
</article>
i write this kind of code even i know it's not good but don't have other option
$html = new simple_html_dom();
$link=array();
foreach ($html->find('article img') as $i => $element) {
$links[$i]['src'] = $element->src;
}
$element = $html->find('article span[class=cat] a');
for ($i = 0; $i <= sizeof($element) / 2 - 1; $i++) {
$links[$i]['cat'][2 * $i + 1] = $element[2 * $i + 1]->href;
$links[$i]['cat'][2 * $i] = $element[2 * $i]->href;
}
cat items are not fixed by doing some have one some have two or more
and some have any, there many fields like this
I want to do something like this for performance and easier parsing but don't know how
$arti=$html->find('article')
foreach ($arti as $i => $element) {
$links[$i]['src'] = $element->img->src;
foreach ($arti->find('cat') as $j => $element) {
$links[$i]['cat'][$j] = $element->href;
}
}
there is a workaround but not perfect answer,my solution is to get outtertext or html of each article and create other instance using simple_htm_dom
$html2 = str_get_html($html->find('article')->outertext);
and then send html2 to other function to parse it .
it not perfect answer so waiting for better answers

Using DomDocument to involve a element with another element?

I have tree divs inside a father div called results_all as you can see below:
<div id="results_all">
<div class="result_information">
</div>
<div class="result_information">
</div>
<div class="result_information">
</div>
</div>
all I want to do is involve all divs whose class is called result_information, with the following code <tr><th>.
so that the final results to be:
<tr><th>
<div class="result_information">
</div>
</th></tr>
<tr><th>
<div class="result_information">
</div>
</th></tr>
<tr><th>
<div class="result_information">
</div>
</th></tr>
How I can do this kind of thing using DomDocument with PHP?
This will match the class using DOMXPath, it's then just a case of outputting the matched content with the appropriate tags when you loop over the results.
$html = '<div id="results_all">
<div class="result_information">
test3
</div>
<div class="result_information">
test2
</div>
<div class="result_information">
test
</div>
</div>
';
$classname = 'result_information';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[contains(#class, '$classname')]");
for($i = 0; $i < $results->length; $i++ ){
print '<tr><th><div class="result_information">' . $results->item($i)->nodeValue . '</div></tr></th>';
}

Find and replace html(Zend_Dom_Query+createElement()): Call to a member function createElement() on a non-object

I need to find and replace some html elements inside an html code (I followed this answer: Getting an element from PHP DOM and changing its value), to do so I retrieve the content with:
$transport = $observer->getTransport();
$html = $transport->getHtml();
$dom = new Zend_Dom_Query($html);
$document = $dom->getDocument();
and this is the result:
<div class="page-title category-title">
<h1>Title</h1>
</div>
<div class="category-products">
<div class="toolbar">
<div class="pager">
<p class="amount">Items 2 to 2 of 2 total</p>
<div class="limiter">
<label>Show</label>
<select onchange="setLocation(this.value)">
<option value="limit=1" selected="selected">1</option>
</select>per page</div>
<div class="pages"> <strong>Page:</strong>
<ol>
<li>
<a class="previous i-previous" href="p=1" title="Previous">
<img src="skin/frontend/default/default/images/pager_arrow_left.gif" alt="Previous" class="v-middle" />
</a>
</li>
<li>1
</li>
<li class="current">2</li>
</ol>
</div>
</div>
<div class="sorter">
<p class="view-mode">
<label>View as:</label> <strong title="Grid" class="grid">Grid</strong> List </p>
<div class="sort-by">
<label>Sort By</label>
<select onchange="setLocation(this.value)">
<option value="dir=asc&order=position" selected="selected">Position</option>
<option value="dir=asc&order=name">Name</option>
<option value="dir=asc&order=price">Price</option>
</select> <img src="skin/frontend/default/default/images/i_asc_arrow.gif" alt="Set Descending Direction" class="v-middle" />
</div>
</div>
</div>
<ul class="products-grid">
<li class="item first">
<a href="test/a-2.html" title="a" class="product-image">
<img src="media/catalog/product/cache/1/small_image/135x/9df78eab33525d08d6e5fb8d27136e95/images/catalog/product/placeholder/small_image.jpg" width="135" height="135" alt="a" />
</a>
<h2 class="product-name">a</h2>
<div class="price-box"> <span class="regular-price" id="product-price-2">
<span class="price">$1.00</span> </span>
</div>
<div class="actions">
<button type="button" title="Add to Cart" class="button btn-cart" onclick="setLocation('test/a-2.html')"><span><span>Add to Cart</span></span>
</button>
<ul class="add-to-links">
<li>
Add to Wishlist
</li>
<li>
<span class="separator">|</span> Add to Compare
</li>
</ul>
</div>
</li>
</ul>
<script type="text/javascript">
decorateGeneric($$('ul.products-grid'), ['odd', 'even', 'first', 'last'])
</script>
<div class="toolbar-bottom">
<div class="toolbar">
<div class="pager">
<p class="amount">Items 2 to 2 of 2 total</p>
<div class="limiter">
<label>Show</label>
<select onchange="setLocation(this.value)">
<option value="limit=1" selected="selected">1</option>
</select>per page</div>
<div class="pages"> <strong>Page:</strong>
<ol>
<li>
<a class="previous i-previous" href="p=1" title="Previous">
<img src="skin/frontend/default/default/images/pager_arrow_left.gif" alt="Previous" class="v-middle" />
</a>
</li>
<li>1
</li>
<li class="current">2</li>
</ol>
</div>
</div>
<div class="sorter">
<p class="view-mode">
<label>View as:</label> <strong title="Grid" class="grid">Grid</strong> List </p>
<div class="sort-by">
<label>Sort By</label>
<select onchange="setLocation(this.value)">
<option value="dir=asc&order=position" selected="selected">Position</option>
<option value="dir=asc&order=name">Name</option>
<option value="dir=asc&order=price">Price</option>
</select> <img src="skin/frontend/default/default/images/i_asc_arrow.gif" alt="Set Descending Direction" class="v-middle" />
</div>
</div>
</div>
</div>
</div>
To find the lements I use Zend_Dom_Query:
$transport = $observer->getTransport();
$html = $transport->getHtml();
$dom = new Zend_Dom_Query($html);
$document = $dom->getDocument();
if(!is_object($document)){
Mage::log(print_r($document, TRUE), null, 'mylogfile1.log');
$transport->setHtml($html);
exit();
}
$node = $document->createElement("p", "This product isn't available in your country.");
Unfortunately it always exit in obeject check otherwise it returns this error:
Fatal error: Call to a member function createElement() on a non-object
EDIT
Full code, if anyone wants to see where I retrieve content (I have added some comments to be more clear):
//retrieve html from observer
$transport = $observer->getTransport();
$html = $transport->getHtml();
//Retrieve other info
$stored = json_decode(Mage::getStoreConfig('razorphyn/country/buttons'));
$theme=trim(Mage::getSingleton('core/design_package')->getTheme('frontend'));
$dom = new Zend_Dom_Query($html);
$document = $dom->getDocument();
//check if $document is an object
if(!is_object($document)){
Mage::log(print_r($document, TRUE), null, 'mylogfile1.log');
$transport->setHtml($html);
exit();
}
//Create node that will replace the finded ones
$node = $document->createElement("p", "This product isn't available in your country.");
$elArray=array();
$productsIds= array();
//Retrieve products id if button and store query results
if($stored[$theme]['isOnClick']){
$queryDom='button'.$stored[$theme]['class'].'[onclick*="/checkout/cart/add/"]';
$results = $dom->query($queryDom);
foreach ($results as $result) {
preg_match("/checkout\/cart\/add.+\/([0-9]+)\//",$result->getAttribute('onclick'),$currentProdId);
$elArray[$currentProdId[0]]=$result;
$productsIds[]=$currentProdId[0];
}
}
//Retrieve products id if form, runa nother query to find button and store query results
else{
$queryDom='form'.$stored[$theme]['formId'].'[action*="/checkout/cart/add/"]';
$results = $dom->query($queryDom);
foreach ($results as $result) {
preg_match("/checkout\/cart\/add.+\/([0-9]+)\//",$result->getAttribute('action'),$currentProdId);
if($currentProdId[0] && is_numeric($currentProdId[0])){
$productsIds[]=$currentProdId[0];
$formDOM = new Zend_Dom_Query($result);
$formButton = $dom->query('button'.$stored[$theme]['class']);
foreach($formButton as $child){
$elArray[$currentProdId[0]]=$child;
}
}
}
}
//Retrieve info from table
$collection = Mage::getModel('razorphyn_country/product')->getCollection()
->addFieldToFilter('active', 1)
->addAttributeToFilter('productId', array('in' => $productsIds));
$res = $collection->getFirstItem();
$country = Mage::getSingleton('core/session')->getCustomerCountry;
//Replace items
if(isset($res->allowed)){
foreach($collection as $res){
if(isset($res->allowed) && (($res->allowed==0 && strpos($res->country, $country) !== false) || ($res->allowed==1 && strpos($res->country, $country) === false))){
$document = $document->replaceChild($node,$elArray[$res->productId]);
}
}
}
//Return edited html
$html = $document->saveHTML();
$transport->setHtml($html);
You realise you are not selecting an element but creating one with createElement()?
I'm not sure where you want to place the paragraph, but let's say in the div.category-products. So let's try something like this;
$transport = $observer->getTransport();
$html = $transport->getHtml();
$dom = new DOMDocument();
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//div[#class="category-products"]');
foreach($nodes as $node) {
$newNode = $dom->createElement("p", "This product isn't available in your country.");
$node->insertBefore($newNode, $node->nextSibling());
}

Parsing page source using PHP

I'm having a lot of trouble parsing the page source of a results page. The results page returns data about businesses in a city. This data includes name, address, phone number, owner name and URL. Any help would be much appreciated.
This is an example of one of the results (There are hundreds in the original file):
<div class="ListingResults_All_CONTAINER ListingResults_Level3_CONTAINER">
<div class="ListingResults_Level3_HEADER">
<div class="ListingResults_All_ENTRYTITLERIGHT">
<div><img src="/external/wcpages/images/L3more.gif" alt="317 at Montgomery"></div>
</div>
<div class="ListingResults_All_ENTRYTITLELEFT">
<div class="ListingResults_All_ENTRYTITLELEFTBOX"><strong><span itemprop="name">317 at Montgomery</span></strong></div>
</div>
</div>
<div class="ListingResults_Level3_MAIN">
<div class="ListingResults_Level3_MAINRIGHT">
<div class="ListingResults_Level3_MAINRIGHTBOX">
<div class="ListingResults_Level3_LOGO"><img src="http://www.centerstateceo.com/external/wcpages/wcwebcontent/webcontentpage.aspx?contentid=2071" class="ListingResults_Level3_LOGOIMG"><div style="width:100%;height:1px;overflow:hidden;"></div>
</div>
<div class="ListingResults_MAINRIGHTBOXDIVIDER" style="width:100%;overflow:hidden;height:1px;">_</div>
<div class="ListingResults_Level3_AFFILIATIONS"></div>
</div>
</div>
<div class="ListingResults_Level3_MAINLEFT">
<div class="ListingResults_Level3_MAINLEFTBOX" itemtype="http://data-vocabulary.org/Address" itemscope="" itemprop="address"><span itemprop="street-address">317 Montgomery St.</span><br><span itemprop="locality">Syracuse</span>, <span itemprop="region">NY</span> <span itemprop="postal-code">13202 </span><div class="ListingResults_Level3_MAINCONTACT"><img src="/external/wcpages/images/maincontact.gif" alt="Mr. Dean Whittles">Mr. Dean Whittles</div>
<div class="ListingResults_Level3_PHONE1"><img src="/external/wcpages/images/phone.gif" alt="Work Phone: (315) 214-4267">(315) 214-4267</div>
</div>
</div>
</div>
<div class="ListingResults_Level3_FOOTER">
<div class="ListingResults_Level3_DESCRIPTION">
<div class="ListingResults_Level3_DESCRIPTIONBOX"></div>
</div>
<div class="ListingResults_Level3_FOOTERRIGHT">
<div class="ListingResults_Level3_FOOTERRIGHTBOX">
<div class="ListingResults_Level3_SOCIALMEDIA"></div>
</div>
</div>
<div class="ListingResults_Level3_FOOTERRIGHT">
<div class="ListingResults_Level3_FOOTERRIGHTBOX">
<div class="ListingResults_Level3_COUPONS"></div>
</div>
</div>
<div class="ListingResults_Level3_FOOTERLEFT">
<div class="ListingResults_Level3_FOOTERLEFTBOX"><span class="ListingResults_Level3_LEARNMORE"><a href="/Restaurants/317-at-Montgomery-7897" class="level3_footer_left_box_a friendly">
Learn More
</a></span><span class="ListingResults_Level3_VISITSITE"> | <a href="http://www.317syr.com" onclick="recordReferralOnClick('20947', '7897', 'W');" target="_blank">
Visit Site
</a></span><span class="ListingResults_Level3_MAP"> | Show on Map</span></div>
</div>
</div>
</div>
PHP Code from Comment:
<?php
$dom = new DOMDocument();
$dom->loadHtml($data);
$spans = $dom->getElementsByTagName('span');
foreach ($spans as $el) {
$children = $el->childNodes->item(1);
if (is_object($children) AND $children->tagName == 'a') {
$url = $children->getAttribute('href');
echo $url;
continue;
}
$user_param = $el->getAttribute('itemprop');
$value = $el->nodeValue;
if ($user_param != "") {
echo $user_param . " " . $value . "\n";
}
}
?>

Creating Html Element from multidimensional Array

Not sure how will i able to explain this, but here we go
I have an array as below:
$arr = array(
array('id' => 1,
'content' => '<h1>This is a simple Content</h1>',
'vars' => null,
'path' => 'holders/row.php',
'sub-page' => array(
'id' => 2,
'content' => '<h1>Sample Body Here</h1>',
'vars' => '{"title":"Sample Title Here"}',
'path' => 'holders/box.php',
'sub-page' => array(
'id' => 3,
'vars' => '{"title":"Sample Title NUmber 3 Here"}',
'content' => '<h1>Again Sample Body Here</h1>',
'path' => 'holders/box.php'
)
)
)
);
What i am looking for is
array should go to the deepest sub-page and json_decode the vars key and extract as variables and include the file.
Once this is done, the output is passed to the parent's content key and parse it again
This is basically for creating dynamic content
the holders/row.php contains:
<div class="row">
<?= $content; ?>
</div>
and holders/box.php contains:
<div class="box lite">
<div class="box-title">
<h4><?= $title; ?></h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<?= $content; ?>
</div>
</div>
and the desired output is:
<div class="row">
<div class="box lite">
<div class="box-title">
<h4>Sample Title Here</h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<h1>Again Sample Body Here</h1>
<div class="box lite">
<div class="box-title">
<h4>Sample Title NUmber 3 Here</h4>
<div class="tools">
<a href="javascript:;" class="collapse">
<i class="fa fa-chevron-up"></i>
</a>
</div>
</div>
<div class="box-body">
<h1>Again Sample Body Here</h1>
</div>
</div>
</div>
</div>
</div>
What have i tried so far
I am trying to loop around the array as below:
foreach ($arr as &$page) {
if (isset($page[$keyToParse])) {
$subMenu = $page[$keyToParse];
unset($page[$keyToParse]);
$page['content'] = $page['content'] . $this->parsePage($keyToParse, $subMenu);
return $page['content'];
} else {
$params = strlen($page['vars']) > 0 ? json_decode($page['vars'], true) : [];
$elementPath = $page['path'];
$params = array_merge($params, array('content' => $page['content']));
$page['content'] = callback(function () use ($params, $elementPath) {
extract($params);
include($elementPath);
});
return $page['content'];
}
and the function callback is as:
function callback($userfunc)
{
ob_start();
$userfunc();
$return = ob_get_contents();
ob_end_clean();
return $return;
}
Please help as i am stuck for a long time now
All you should have to do is something like this.
function renderNode($node){
ob_start();
if (isset($node["sub-page"]) && $node["sub-page"]){
$node["content"] = renderNode($node["sub-page"]);
}
extract($node);
include ($node["path"]);
return ob_get_clean();
}
and then invoke it like:
echo renderNode($arr[0]);

Categories