how make parse in some ul? - php

<ul>
<li>
<a>name1</a>
<div>
<ul>
<li>
<a>name</a>
<ul>
<li>
<a>name</a>
</li>
<li>
<a>name</a>
</li>
</ul>
</li>
<li>
<a>name</a>
<ul>
<li>
<a>name</a>
</li>
<li>
<a>name</a>
</li>
</ul>
</li>
</ul>
</div>
</li>
<li>
<a>name2</a>
<div>
<ul>
<li>
<a>name</a>
<ul>
<li>
<a>name</a>
</li>
<li>
<a>name</a>
</li>
</ul>
</li>
<li>
<a>name</a>
<ul>
<li>
<a>name</a>
</li>
<li>
<a>name</a>
</li>
</ul>
</li>
</ul>
</div>
</li>
</ul>
As we can se we hve some <ul>.
We would like use PHP Simple HTML DOM for get array with data ul up.
We want use code:
foreach($html->find('li') as $li) {
}
But in this example we see all li on display:
http://prntscr.com/5390mf
http://prntscr.com/5390pj
But we dont know how get only parrent li:
1.
<li>
<a>name1</a>
</li>
2.
<li>
<a>name2</a>
</li>
And only than get childrens ul in parrents li and all children li in li.
Tell me please how make it?
P.S.: if i do bad explain that I would like to receive, please write

First:
foreach($html->find('li',0) as $line) {
}
Second (if we can add class to parrent ul):
foreach($html->find('ul.parrent_ul->li') as $line) {
}
Enjoy!

foreach($html->find('ul > li') as $li) {
}
Should get only the top level li's - the one's that are direct children of the ul elements

Related

add subdirector for my org chart

This is source code: and i want customize it, but i dont know how add "textgrandchild"
https://bootsnipp.com/snippets/eN3Q8
and also below is my source code:
<!--
We will create a family tree using just CSS(3)
The markup will be simple nested lists
-->
<div class="tree">
<ul>
<li>
Parent
<ul>
<li>
Child
<ul>
<li>
Grand Child
</li>
</ul>
</li>
<li>
Child
<ul>
<li>Grand Child</li>
<li>
Grand Child
<ul>
<li>
Great Grand Child
</li>
<li>
Great Grand Child
</li>
<li>
Great Grand Child
</li>
</ul>
</li>
<li>Grand Child</li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
And when i customize, now i want add like 'Great Grand Child" in source in Myself customize, but i don't know how add ul and li
Can you add one "Great Grand Child" for each on my "textgrandchild" in below code pen?
So ill understand
my customized is below
https://codepen.io/anon/pen/dVQWVN

Scrape HTML list structure with PHP

I want to scrape a html list structure, so I can save parent and child separately.
Here's the view source of html
<ul class="categories_list">
<li>Sports Nutrition
<ul class="categories_list">
<li>Protein
<ul class="categories_list">
<li>Protein Powder
<ul class="categories_list">
<li>Whey Protein
<ul class="categories_list">
<li>Whey Protein Isolate</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="categories_list">
<li>Pre Workout Supplements</li>
</ul>
<ul class="categories_list">
<li>Creatine
<ul class="categories_list">
<li>Creatine Monohydrate</li>
</ul>
</li>
</ul>
<ul class="categories_list">
<li>Amino Acids
<ul class="categories_list">
<li>Essential Amino Acids
<ul class="categories_list">
<li>BCAA</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="categories_list">
<li>Joint Supplements
<ul class="categories_list">
<li>Curcumin
<ul class="categories_list">
<li>Curcumin Phytosome</li>
</ul>
</li>
</ul>
</li>
</ul>
<ul class="categories_list">
<li>Energy & Endurance
<ul class="categories_list">
<li>Stimulants</li>
</ul>
</li>
</ul>
</li>
</ul>
I am using simple HTML DOM for scraping. I am able to get all categories, but I cannot get them in proper the hierarchy.
I also tried the children approach, but that didn't work.
So I am looking for some help in my existing to make it working.
Here's my existing code:
$html= file_get_html($url);
foreach ($html->find('ul.categories_list li') as $link) {
echo $link->plaintext.'<br>';
}
There is this script which tried to get all elements. This needs to be improved upon:
<?php
require_once("simple_html_dom.php");
$dom = file_get_html("source.php");
getCategory($dom);
print_r($categoryList);
function getCategory(simple_html_dom $dom){
global $categoryList;
foreach($dom->find('ul.categories_list li') as $ul){
//extract the a tag if found
$categoryName = $ul->find('a',0)->href;
$categoryLabel = $ul->find('a',0)->innertext;
$categoryList[] = array(
"categoryName" => $categoryName,
"categoryLabel" => $categoryLabel,
);
//remove a node
$ul->find('a',0)->outertext = '';
$string = $ul->innertext;
if(trim($string) == ''){
continue;
}else{
// die($string);
$dom2 = str_get_html($string);
getCategory($dom2);
}
}
}
It basically does recursion filling the $categoryList on each call.

Php Sub Category loop html

I get the data from the database. but I can not write between li,ul html tags.There is a problem in the loop. How do I write between li ul categories and subcategories. I have this pdo mysql category function.
<?php
...
function kategoriVer($ustid=0){
$result = DB::get('SELECT * FROM urunler_kategori WHERE kategori_k='.$ustid.'');
echo '<ul class="drop-down">';
foreach($result as $kategori){
echo '<li class="drop">
'.$kategori->baslik_k.'';
kategoriVer($kategori->id_k);
}
echo '</li></ul>';
}
kategoriVer();
?>
output html:
<ul class="sub-menu">
<li class="menuparent">
Çadırlar
<ul class="sub-menu">
<li class="menuparent">Hi-Tech Çadırlar<ul class="sub-menu"></li></ul>
<li class="menuparent">Çelik Konstrüksiyon<ul class="sub-menu"></li></ul>
<li class="menuparent">Tribün Çadırlar<ul class="sub-menu"></li></ul>
<li class="menuparent">Yürüyüş Yolları<ul class="sub-menu"></li></ul>
</li>
</ul>
<li class="menuparent">Şemsiyeler<ul class="sub-menu"></li></ul>
<li class="menuparent">İklimlendirme<ul class="sub-menu"></li></ul>
</li>
</ul>
i want this output:
<ul class="sub-menu">
<li class="menuparent">
Headers
<ul class="sub-menu">
<li>Standard</li>
<li>No Topbar</li>
<li>Social Icons</li>
<li>Minimal</li>
<li>Classic</li>
</ul>
</li>
</ul>

regex for proper nesting of dynamic menu

I've a dynamic menu which looks like
<li class='has-sub'> cat1</li>
<ul>
<li> test5</li>
<li class='has-sub'> cat2</li>
<ul>
<li> cat9</li>
<li class='has-sub'> cat7</li>
<ul>
<li> cat8</li>
<li> cat10</li>
<li> cat1 cat2</li>
</ul>
</ul>
</ul>
<li class='has-sub'> cat3</li>
<ul>
<li> cat5</li>
</ul>
I want to change that to a properly nested navigation menu like
<li class='has-sub'> <a href='#'><span>cat1</span></a>
<ul>
<li><a href='#'><span> test5</span></a></li>
<li class='has-sub'><a href='#'><span> cat2</span></a>
<ul>
<li> <a href='#'><span>cat9</span></a></li>
<li class='has-sub'> <a href='#'><span>cat7</span></a>
<ul>
<li> <a href='#'><span>cat8</span></a></li>
<li> <a href='#'><span>cat10</span></a></li>
<li> <a href='#'><span>cat1 cat2</span></a></li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
<li class='has-sub'> <a href='#'><span>cat3</span></a>
<ul>
<li> <a href='#'><span>cat5</span></a></li>
</ul>
</li>
I tried few str_replace but since the list is dynamic It wont work.
I'm new to Regex and am not sure how to format this dynamic menu to a properly nested/formatted menu.
Thanks in advance!
It's an answer that has been linked to more than it probably has been read, but still: You can't parse markup using regex. Not reliably anyway.
Instead, you should use a parser like the DOMDocument class. Basic usage here would be:
$dom = new DOMDocument();
$dom->loadHTML($theMarkupString);
//get the list:
$list = $dom->getElementById('navContainerID');
$navItems = $list->getElementsByTagName('li');
foreach($navItems as $item)
{
//add spans, links, classes... how to do so is all in the doc pages
}
A simple DOM Parser and an strtr() will solve this...
$dom = new DOMDocument;
$dom->loadHTML($html);
$arrLi = array();
foreach ($dom->getElementsByTagName('li') as $tag) {
$arrLi[$tag->nodeValue]="<a href='#'><span>$tag->nodeValue</span></a>";
}
echo $html = strtr($html,$arrLi);
Demonstration

How to un-nest nested lists in php

I have a nested unordered list like this (simplified version / the depth is variable) :
<ul>
<li>
Root
<ul>
<li>
Page A
<ul>
<li>
Page 1 2
</li>
</ul>
</li>
</ul>
</li>
</ul>
Using PHP, is there a nice way to "explode" this nested list in (for this example) 3 lists ?
Thanks for your help
Edit :
The expected output will be :
<ul>
<li>
Root
</li>
</ul>
<ul>
<li>
Page A
</li>
</ul>
<ul>
<li>
Page 1 2
</li>
</ul>
Try:
$lists = '<ul>
<li>
Root
<ul>
<li>
Page A
<ul>
<li>
Page 1 2
</li>
</ul>
</li>
</ul>
</li>
</ul>';
$list_array = explode('<ul>', $lists);
foreach($list_array as $list){
// Now you have a single list, but missing the <ul>
// at the start. replace that and assign to a variable or whatever.
}

Categories