PHP Simple HTML DOM Parser - php

I just started to use PHP Simple HTML DOM Parser.
Now I'm trying to extract all elements surrounded with a <b>-tag inclduing </b> from an exsiting HTML document. This works fine with
foreach($html->find('b') as $q)
echo $q;
How can I achieve to show up only elements surrounded with the <b>,</b>-tags followed by a <span class="marked">?
Update:
I've used firebug to get the css path for the elements. Now it looks like this:
foreach ($html->find('html body div#wrapper table.desc tbody tr td div span.marked') as $x)
foreach ($x->find('html body div#wrapper table.desc tbody tr td table.split tbody tr td b') as $d)
echo $d;
But it won't work... Any Ideas?
Update:
To clarify my question here a sample tr of the document with starting table and ending table tags.
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="desc">
<tr>
<th width="25%" scope="col"><div align="center">1</div></th>
<th width="50" scope="col"><div align="center">2</div></th>
<th width="10%" scope="col"><div align="center">3</div></th>
<th width="15%" scope="col"><div align="center">4</div></th>
</tr>
<tr>
<td valign="top" bgcolor="#E9E9E9"><div style="text-align: center; font-weight: bold; margin-top: 2px"> 1 </div></td>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="split"> <tr>
<td>
<b> element to extract</b></td>
</tr>
<tr>
<td>
<table width="100%" border="0" cellspacing="0" cellpadding="0" class="split"> <tr>
<td width="15px" valign="top"> </td>
<td width="15px" valign="top">
<div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px">
1
</div>
</td>
<td>
abed
</td>
</tr>
<tr>
<td width="15px" valign="top"> </td>
<td width="15px" valign="top">
<div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px">
2
</div>
</td>
<td>
ddee
</td>
</tr>
<tr>
<td width="15px" valign="top"> </td>
<td width="15px" valign="top">
<div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px">
3
</div>
</td>
<td>
xdef
</td>
</tr>
<tr>
<td width="15px" valign="top"> </td>
<td width="15px" valign="top">
<div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px">
4
</div>
</td>
<td>
abbcc
</td>
</tr>
<tr>
<td width="15px" valign="top"> </td>
<td width="15px" valign="top">
<div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px">
5
</div>
</td>
<td>
ab
</td>
</tr>
<tr>
<td width="15px" valign="top"> </td>
<td width="15px" valign="top">
<div style="background-color:green ;color:#FFFFFF; text-align:center;padding-bottom: 1px">
6
</div>
</td>
<td>
e1
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td valign="top"><div style="text-align: center"> <span class="marked">marked</span> </div></td>
<td valign="top"><div style="text-align: center"> </div></td>
</tr>
</table>

Try the following CSS selector
b > span.marked
That would return the span though, so you probably have to do $e->parent() to get to the b element.
Also see Best Methods to parse HTML for alternatives to SimpleHtmlDom
Edit after update:
Your browser will modify the DOM. If you look at your markup, you will see that there is no tbody elements. Yet Firebug gives you
html body div#wrapper table.desc tbody tr td div span.marked'
html body div#wrapper table.desc tbody tr td table.split tbody tr td b'
Also, your question does not match the queries. You asked how to find
elements surrounded with the <b>,</b>-tags followed by a <span class="marked">
That can be read to either mean
<b><span class="marked">foo</span></b>
or
<b><element>foo</element></b><span class="marked">foo</span>
For that first use the child combinator I have shown earlier. For the second, use the adjacent sibling combinator
b + span.marked
to get the span and then use $e->prev_sibling() to return the previous sibling of element (or null if not found).
However, in your shown markup, there is neither nor. There is only a DIV with a SPAN child having the marked class
<div style="text-align: center"> <span class="marked">marked</span>
If that is what you want to match, it's the child combinator again. Of course, you have to change the b then to a div.

More simple is from manual:
foreach($html->find('b') as $q)
echo $q->plaintext;

Related

How to do make html foreach table?

I tried to do it but the big problem is foreach here i have the foto:
Problem
Here is my table picture
Here is the code but still have problem:
<div class="row">
<table class="table table-bordered table-striped table-highlight">
<tbody>
<tr height="100">
<td rowspan="6" height="120" width="3%" style="vertical-align: middle;">
<p style="white-space: nowrap;writing-mode: vertical-lr;transform: rotate(180deg);font-weight: bold;"><font >Here is my title </font></p>
</td>
<td colspan="3">
<font>Name</font>
</td>
<td width="64">
<font>Article</font>
</td>
<td width="64">
<font>Price</font>
</td>
<td width="64">
<font>Date</font>
</td>
</tr>
<tr height="20">
<td colspan="3" rowspan="5" height="120" ></td>
<td rowspan="5" style="vertical-align: top;"></td>
<td rowspan="5" style="vertical-align: top;"></td>
<td rowspan="5" style="vertical-align: top;"></td>
</tr>
<tr height="15">
</tr>
<tr height="15">
</tr>
<tr height="15">
</tr>
<tr height="15">
</tr>
</tbody>
</table>
</div>
How can i do it so that when i use foreach it will insert new td and the vertical Here is my title will be there?
ok, so you're using php here's a very basic example on how you can do that, i'm assuming you already did query and the fetching part, and this is a guess of your data structure, you can adjust it to your liking :
<div class="row">
<table class="table table-bordered table-striped table-highlight">
<tbody>
<tr height="100">
<td rowspan="100%" height="120" width="3%" style="vertical-align: middle;">
<p style="white-space: nowrap;writing-mode: vertical-lr;transform: rotate(180deg);font-weight: bold;"><font >Here is my title </font></p>
</td>
<td colspan="3">
<font>Name</font>
</td>
<td width="64">
<font>Article</font>
</td>
<td width="64">
<font>Price</font>
</td>
<td width="64">
<font>Date</font>
</td>
</tr>
<?php
foreach($data as $key => $value){
echo '<tr height="20">';
echo '<td colspan="3">'.$value['name'].'</td>';
echo '<td width="64" style="vertical-align: top;">'.$value['article'].'</td>';
echo '<td width="64" style="vertical-align: top;">'.$value['price'].'</td>';
echo '<td width="64" style="vertical-align: top;">'.$value['date'].'</td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
here's a fiddle : https://jsfiddle.net/kkpjx328/
I hope this helps .
PS : on your next question, please consider adding the data structure and if possible, an example or code sample of what you've tried so far so we can provide more specific answers

set variable to element from other site (PHP)

I'm not the best at explaining things but here I go,
On a different site, I have 10 elements that i want to get and assign to variables
this is how they are
element 1:
<div class="username_header dynamic_fontsize" style="font-size: 48px; top: 0px;">
<span class="username">[element 1]</span>
<span class="user_online_msg">stuff</span>
</div>
elements 2-10
<div class="forum_profile_right_block">
<table border="0" width="97%" cellspacing="2" cellpadding="5">
<tbody>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc2</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">[element 2]</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc34</div>
</td>
<td align="center" bgcolor="#F0F0F0">[element 3]</td>
<td align="center" bgcolor="#F0F0F0">[element 4]</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc5</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">[element 5]</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc6</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">[element 6]
</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc7</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">[element 7]</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc8</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">[element 8]</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc9</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">
<div style="display:inline-block;">[element 9]</div>
<div style="display: inline-block; position: relative; top: 1px;"><img src="images/msdropdown/icons/blank.gif" class="flag us"></div>
</td>
</tr>
<tr>
<td>
<div style="font-color:black; font-size:1em">abc10</div>
</td>
<td colspan="2" align="center" bgcolor="#F0F0F0">[element 10]
</td>
</tr>
</tbody>
</table>
<br>
</div>

Getting value of specific td in selected row

I have the way to get the value of the td's at the time the row is selected using
$(this).find('.servid').val()
However I cannot find the way to get this value later.
<table id="servicetable" class="scroll" style="border: 1px solid #cbcbcb;" align="center">
<tbody>
<tr class="selected">
<td>Service</td>
<td class="servid" value="4004072">72569000</td>
<td class="origin">PAC</td>
<td class="street">60 KENDAL</td>
<td class="city">SANRDINO</td>
<td class="state">CA</td>
<td class="zip">99999</td>
</tr>
<tr>
<td>TelePacific Circuit</td>
<td class="servid" value="5369592">77051900</td>
<td class="origin">TP</td>
<td class="street">819 KAISER</td>
<td class="city">AHEM</td>
<td class="state">CA</td>
<td class="zip">88888</td>
</tr>
</tbody>
</table>
I need to later, after filling out more of the form, get the val() of the selected tr -> servid td
I have tried various things but they are not working
$('#servicetable .selected > td:nth-child(2)').val();
$('#servicetable').find('.selected > td:nth-child(2)').val();
$('#servicetable').find('tr.selected').find('.servid').val();
Below goes your sollution
alert($('#servicetable tr.selected > td.servid').attr('value'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="servicetable" class="scroll" style="border: 1px solid #cbcbcb;" align="center">
<tbody>
<tr class="selected">
<td>Service</td>
<td class="servid" value="4004072">72569000</td>
<td class="origin">PAC</td>
<td class="street">60 KENDAL</td>
<td class="city">SANRDINO</td>
<td class="state">CA</td>
<td class="zip">99999</td>
</tr>
<tr>
<td>TelePacific Circuit</td>
<td class="servid" value="5369592">77051900</td>
<td class="origin">TP</td>
<td class="street">819 KAISER</td>
<td class="city">AHEM</td>
<td class="state">CA</td>
<td class="zip">88888</td>
</tr>
</tbody>
</table>
One more thing you can not put value like this as it will not validate as per w3c, recommanded to put data-value, if HTML5.
And in jquery also you can put like
alert($('#servicetable tr.selected > td.servid').attr('data-value'));
$('#value1').text($('#servicetable .selected > td:nth-child(2)').attr('value'));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<table id="servicetable" class="scroll" style="border: 1px solid #cbcbcb;" align="center">
<tbody>
<tr class="selected">
<td>Service</td>
<td class="servid" value="4004072">72569000</td>
<td class="origin">PAC</td>
<td class="street">60 KENDAL</td>
<td class="city">SANRDINO</td>
<td class="state">CA</td>
<td class="zip">99999</td>
</tr>
<tr>
<td>TelePacific Circuit</td>
<td class="servid" value="5369592">77051900</td>
<td class="origin">TP</td>
<td class="street">819 KAISER</td>
<td class="city">AHEM</td>
<td class="state">CA</td>
<td class="zip">88888</td>
</tr>
</tbody>
</table>
<label id="value1"/>
IDEAL WAY
use data attribute as shown below,
<td class="servid" data-value="4004072">72569000</td>
$('#value1').text($('#servicetable .selected > td:nth- child(2)').data('value'));
It might be better if you tag the TR with the id you need and then have the "attributes" in the TDs.
You can then parse TDs based on TR
<tr class="selected" id="98989">
<td class="city">Lisbon</td>
.....
_tr = $("#servicetable .selected");
_trId=_tr.attr("id");
_trCity = _tr.find(".city").text();
.....

PHP DOM get element which contains

Need help with parsing HTML code by PHP DOM.
This is simple part of huge HTML code:
<table width="100%" border="0" align="center" cellspacing="3" cellpadding="0" bgcolor='#ffffff'>
<tr>
<td align="left" valign="top" width="20%">
<span class="tl">Obchodne meno:</span>
</td>
<td align="left" width="80%">
<table width="100%" border="0">
<tr>
<td width="67%">
<span class='ra'>STORE BUSSINES</span>
</td>
<td width="33%" valign='top'>
<span class='ra'>(od: 02.10.2012)</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
What I need is to get text "STORE BUSINESS". Unfortunately, the only thing I can catch is "Obchodne meno" as a content of first tag, so according to this content I need to get its parent->parent->first sibling->child->child->child->child->content. I have limited experience with parsing html in php so any help will be valuable. Thanks in advance!
Make use of DOMDocument Class and loop through the <span> tags and put them in array.
<?php
$html=<<<XCOE
<table width="100%" border="0" align="center" cellspacing="3" cellpadding="0" bgcolor='#ffffff'>
<tr>
<td align="left" valign="top" width="20%">
<span class="tl">Obchodne meno:</span>
</td>
<td align="left" width="80%">
<table width="100%" border="0">
<tr>
<td width="67%">
<span class='ra'>STORE BUSSINES</span>
</td>
<td width="33%" valign='top'>
<span class='ra'>(od: 02.10.2012)</span>
</td>
</tr>
</table>
</td>
</tr>
</table>
XCOE;
$dom = new DOMDocument;
$dom->loadHTML($html);
foreach ($dom->getElementsByTagName('span') as $tag) {
$spanarr[]=$tag->nodeValue;
}
echo $spanarr[1]; //"prints" STORE BUSINESS

open a div having id using onclick in a hyperlink

below is the given div having id "panel" and links in the same page but outside this div, when I click on the link this div should be open. there are several links so jquery does not work and my button is created dynamically through php while loop hence i cannot put unique id in my hyperlink
<div id="panel">
<form name="userloginform" action="xxx.php" method="post">
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center" class="tb">
<tr>
<td align="left" valign="middle" height="80">User Name</td>
<td align="left" valign="middle">: </td>
<td align="left" valign="middle"><input name="user" type="text" class="log"/></td>
</tr>
<tr>
<td align="left" valign="middle">Password</td>
<td align="left" valign="middle">: </td>
<td align="left" valign="middle"><input name="pass" type="password" class="log"/></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
</tr>
<tr>
<td colspan="3"><input name="submit1" type="submit" value="Login" class="login_btn"/></td>
</tr>
</table>
</form>
</div>
and my link is
<a href="#" id="flip" >Launch Now</a>
<a href="#" id="" >Launch Now</a>
If I am not clear please reply which part is not clear so that I can edit and provide useful information clearly
ok here is where link is coming from, a CMS is used here.
<?php
$sql=mysql_query("SELECT * FROM heading ORDER BY id DESC");
while($r=mysql_fetch_array($sql))
{
$ii=$r['id'];
?>
<h3><?php echo $r['heading'];?></h3>
<div>
<div class="content">
<div class="main_table_wrapper">
<table width="1030" border="0" cellspacing="0" cellpadding="0" align="left">
<?php
$sql2=mysql_query("SELECT * FROM inner_table WHERE head='$ii'") ;
while($rows=mysql_fetch_array($sql2))
{
$abc=str_replace($rows['url'],'XXXXXXXXXXXXXXX',$rows['url']);
?>
<tr>
<th align="left" valign="middle" width="350"><?php echo $rows['inner_names'];?></th>
<th align="left" valign="middle" width="250"><?php echo $abc;?></th>
<th align="left" valign="middle" width="200"><?php echo $rows['author'];?></th>
<th align="center" valign="middle" width="100"><?php echo $rows['doe'];?></th>
<th align="right" valign="middle" width="130"><span class="lunch">Launch Now</span></th>
</tr>
<?php } ?>
</table>
</div>
</div>
</div>
<?php } ?>
</div>
You can't have multiple elements with the same id.
Since you didn't show any JS code so far, the only advice I can give is to use a class as the identifier for the action link.
From the look of your markup you could also use something along
$('span.lunch').on('click', 'a', function(e) { //do stuff } );

Categories