PDA

View Full Version : استخراج تگ جدول از html



omid_student
یک شنبه 27 بهمن 1392, 18:12 عصر
سلام دوستان
کدی در زیر قرار دادم میخوام جدولی که id اون با table_ شروع میشه و بقیش عدد هست رو کلا هر چی داخلش هست رو از کدهای زیر جدا کنم
چند بار خواستم از DomDocument استفاده کنم ولی query هاشو بلد نشدم
کد زیر رو ببینید ممنون میشم کمک کنید
فرض کنید جدول مورد نظر من کد زیر هست چطور میتونم این جدول رو با محتویاتش بگیرم بعد چاپش کنم


<table id="table_27798" cellspacing="0" cellpadding="3" style="margin-bottom:10px;" width="98%">
<tbody><tr><td colspan="10">
<table cellpadding="0" cellspacing="0" class="titre_tab" width="100%"><tbody><tr>
<td class="titre_gauche"></td>
<td class="titre_centre"><a href="/ligue1endirect-c123c.php" class="titre_font" style="text-decoration:none;"><span style="white-space:nowrap;">France : Ligue 1</span></a></td>
<td class="titre_droite"></td>
<td align="right"><img src="/images/livescore/moins.png" hspace="2" onclick="return cache_champ(27798);" style="cursor:pointer;"></td>
</tr></tbody></table>
</td></tr><tr id="ligne_932015" class="l1">
<td onclick="return cache(932015, 2);"><img src="/images/livescore/moins_petit.gif"></td>
<td width="50"><span id="date_932015">terminé</span></td>
<td width="50"><img id="alerte_but_932015" src="/images/livescore/alerte_but.png" hspace="4" style="display:none;"></td>
<td align="center" width="160"><span id="joueur1_932015" class="vainqueur">Lyon</span><input type="hidden" id="equipe1_932015" value="3288"></td>
<td align="center" width="160">
<div id="contour_score_932015" class="contour" onclick="return affiche_table(932015);" style="cursor:pointer;">
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr align="center">
<td width="100%"><b><font style="color:#000;font-size:13px;"><span id="score_932015">3 - 1<font class="tres_petit"> (1-0)</font></span></font></b></td>
<td width="15"><span id="voir_buteurs_cartons_932015" style="display:block;"><img src="/images/livescore/plus_detail.png" hspace="3" border="0"></span></td>
</tr></tbody></table>
</div>
<span id="score2_932015"></span>
</td>
<td align="center" width="160"><span id="joueur2_932015">Ajaccio</span></td>
<td align="center" id="td_micro_932015"><img src="/images/livescore/micro.gif"></td>
<td><a href="/lyon-ajaccio-endirect-mf932015.htm"><img id="lien_infos_932015" src="/images/livescore/infos.png" border="0" align="absmiddle" alt="Resultat match Lyon Ajaccio en direct"></a></td>
<td></td><td><a href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')"><img src="/images/pariez.png" align="absmiddle" border="0" alt="Parier"></a></td>
</tr>
<tr id="ligne2_932015" height="2" class="l1">
<td colspan="10">
<div id="extra_932015" style="display:none;" align="center">
<table cellspacing="0" cellpadding="3" style="margin-right:45px;"><tbody><tr><td width="160" valign="top"><div id="buteur1_932015"><div class="butg" id="buteur_4661069">Fofana G. (43)</div><div class="butg" id="buteur_4661196">Briand J. (68)</div><div class="butg" id="buteur_4661280">Gomis B. (89)</div></div></td><td width="70"></td><td width="145" valign="top"><div id="buteur2_932015"><div class="butd" id="buteur_4661244">Oliech D. (81)</div></div></td></tr>
<tr><td width="160" valign="top"><div id="carton1_932015"><div class="CJg" id="carton_801325">Ferri J. (73)</div></div></td><td width="70"></td><td width="145" valign="top"><div id="carton2_932015"><div class="CJd" id="carton_801282">Perozo G. (30)</div><div class="CJd" id="carton_801340">Tonucci D. (79)</div></div></td></tr>
</tbody></table>
</div></td></tr><tr id="ligne_932011" class="l2">
<td onclick="return cache(932011, 2);"><img src="/images/livescore/moins_petit.gif"></td>
<td width="50"><span id="date_932011">17:00</span></td>
<td width="50"><img id="alerte_but_932011" src="/images/livescore/alerte_but.png" hspace="4" style="display:none;"></td>
<td align="center" width="160"><span id="joueur1_932011">Evian</span><input type="hidden" id="equipe1_932011" value="35533"></td>
<td align="center" width="160">
<div id="contour_score_932011" onclick="return affiche_table(932011);" style="cursor:pointer;">
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr align="center">
<td width="100%"><b><font style="color:#000;font-size:13px;"><span id="score_932011"></span></font></b></td>
<td width="15"><span id="voir_buteurs_cartons_932011" style="display:none;"><img src="/images/livescore/plus_detail.png" hspace="3" border="0"></span></td>
</tr></tbody></table>
</div>
<span id="score2_932011"><b><table cellspacing="4" cellpadding="2"><tbody><tr align="center">
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">3.71</a></td>
<td class="contour" width="20"><a class="cotes" href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')">3.25</a></td>
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">2.01</a></td>
</tr></tbody></table></b></span>
</td>
<td align="center" width="160"><span id="joueur2_932011">Lille</span></td>
<td align="center" id="td_micro_932011"><img src="/images/livescore/micro.gif"></td>
<td><a href="/evian-lille-endirect-mf932011.htm"><img id="lien_infos_932011" src="/images/livescore/infos.png" border="0" align="absmiddle" alt="Resultat match Evian Lille en direct"></a></td>
<td></td><td><a href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')"><img src="/images/pariez.png" align="absmiddle" border="0" alt="Parier"></a></td>
</tr>
<tr id="ligne2_932011" height="2" class="l2">
<td colspan="10">
<div id="extra_932011" style="display:none;" align="center">
<table cellspacing="0" cellpadding="3" style="margin-right:45px;"><tbody><tr><td width="160" valign="top"><div id="buteur1_932011"></div></td><td width="70"></td><td width="145" valign="top"><div id="buteur2_932011"></div></td></tr>
<tr><td width="160" valign="top"><div id="carton1_932011"></div></td><td width="70"></td><td width="145" valign="top"><div id="carton2_932011"></div></td></tr>
</tbody></table>
</div></td></tr><tr id="ligne_932021" class="l1">
<td onclick="return cache(932021, 2);"><img src="/images/livescore/moins_petit.gif"></td>
<td width="50"><span id="date_932021">21:00</span></td>
<td width="50"><img id="alerte_but_932021" src="/images/livescore/alerte_but.png" hspace="4" style="display:none;"></td>
<td align="center" width="160"><span id="joueur1_932021">St Etienne</span><input type="hidden" id="equipe1_932021" value="15449"></td>
<td align="center" width="160">
<div id="contour_score_932021" onclick="return affiche_table(932021);" style="cursor:pointer;">
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr align="center">
<td width="100%"><b><font style="color:#000;font-size:13px;"><span id="score_932021"></span></font></b></td>
<td width="15"><span id="voir_buteurs_cartons_932021" style="display:none;"><img src="/images/livescore/plus_detail.png" hspace="3" border="0"></span></td>
</tr></tbody></table>
</div>
<span id="score2_932021"><b><table cellspacing="4" cellpadding="2"><tbody><tr align="center">
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">2.01</a></td>
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">3.21</a></td>
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">3.71</a></td>
</tr></tbody></table></b></span>
</td>
<td align="center" width="160"><span id="joueur2_932021">Marseille</span></td>
<td align="center" id="td_micro_932021"><img src="/images/livescore/micro.gif"></td>
<td><a href="/st-etienne-marseille-endirect-mf932021.htm"><img id="lien_infos_932021" src="/images/livescore/infos.png" border="0" align="absmiddle" alt="Resultat match St Etienne Marseille en direct"></a></td>
<td></td><td><a href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')"><img src="/images/pariez.png" align="absmiddle" border="0" alt="Parier"></a></td>
</tr>
<tr id="ligne2_932021" height="2" class="l1">
<td colspan="10">
<div id="extra_932021" style="display:none;" align="center">
<table cellspacing="0" cellpadding="3" style="margin-right:45px;"><tbody><tr><td width="160" valign="top"><div id="buteur1_932021"></div></td><td width="70"></td><td width="145" valign="top"><div id="buteur2_932021"></div></td></tr>
<tr><td width="160" valign="top"><div id="carton1_932021"></div></td><td width="70"></td><td width="145" valign="top"><div id="carton2_932021"></div></td></tr>
</tbody></table>
</div></td></tr></tbody></table>

MMSHFE
یک شنبه 27 بهمن 1392, 21:27 عصر
من با این کد تست کردم جواب گرفتم:


<?php

/**
* @author M-L-F
* @copyright 2014
*/

$html = <<<EOT
<!doctype html>
<html>
<head>
<title>Test</title>
<meta charset="utf-8" />
</head>
<body>
<table id="table_27798" cellspacing="0" cellpadding="3" style="margin-bottom:10px;" width="98%">
<tbody><tr><td colspan="10">
<table cellpadding="0" cellspacing="0" class="titre_tab" width="100%"><tbody><tr>
<td class="titre_gauche"></td>
<td class="titre_centre"><a href="/ligue1endirect-c123c.php" class="titre_font" style="text-decoration:none;"><span style="white-space:nowrap;">France : Ligue 1</span></a></td>
<td class="titre_droite"></td>
<td align="right"><img src="/images/livescore/moins.png" hspace="2" onclick="return cache_champ(27798);" style="cursor:pointer;"></td>
</tr></tbody></table>
</td></tr><tr id="ligne_932015" class="l1">
<td onclick="return cache(932015, 2);"><img src="/images/livescore/moins_petit.gif"></td>
<td width="50"><span id="date_932015">terminé</span></td>
<td width="50"><img id="alerte_but_932015" src="/images/livescore/alerte_but.png" hspace="4" style="display:none;"></td>
<td align="center" width="160"><span id="joueur1_932015" class="vainqueur">Lyon</span><input type="hidden" id="equipe1_932015" value="3288"></td>
<td align="center" width="160">
<div id="contour_score_932015" class="contour" onclick="return affiche_table(932015);" style="cursor:pointer;">
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr align="center">
<td width="100%"><b><font style="color:#000;font-size:13px;"><span id="score_932015">3 - 1<font class="tres_petit"> (1-0)</font></span></font></b></td>
<td width="15"><span id="voir_buteurs_cartons_932015" style="display:block;"><img src="/images/livescore/plus_detail.png" hspace="3" border="0"></span></td>
</tr></tbody></table>
</div>
<span id="score2_932015"></span>
</td>
<td align="center" width="160"><span id="joueur2_932015">Ajaccio</span></td>
<td align="center" id="td_micro_932015"><img src="/images/livescore/micro.gif"></td>
<td><a href="/lyon-ajaccio-endirect-mf932015.htm"><img id="lien_infos_932015" src="/images/livescore/infos.png" border="0" align="absmiddle" alt="Resultat match Lyon Ajaccio en direct"></a></td>
<td></td><td><a href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')"><img src="/images/pariez.png" align="absmiddle" border="0" alt="Parier"></a></td>
</tr>
<tr id="ligne2_932015" height="2" class="l1">
<td colspan="10">
<div id="extra_932015" style="display:none;" align="center">
<table cellspacing="0" cellpadding="3" style="margin-right:45px;"><tbody><tr><td width="160" valign="top"><div id="buteur1_932015"><div class="butg" id="buteur_4661069">Fofana G. (43)</div><div class="butg" id="buteur_4661196">Briand J. (68)</div><div class="butg" id="buteur_4661280">Gomis B. (89)</div></div></td><td width="70"></td><td width="145" valign="top"><div id="buteur2_932015"><div class="butd" id="buteur_4661244">Oliech D. (81)</div></div></td></tr>
<tr><td width="160" valign="top"><div id="carton1_932015"><div class="CJg" id="carton_801325">Ferri J. (73)</div></div></td><td width="70"></td><td width="145" valign="top"><div id="carton2_932015"><div class="CJd" id="carton_801282">Perozo G. (30)</div><div class="CJd" id="carton_801340">Tonucci D. (79)</div></div></td></tr>
</tbody></table>
</div></td></tr><tr id="ligne_932011" class="l2">
<td onclick="return cache(932011, 2);"><img src="/images/livescore/moins_petit.gif"></td>
<td width="50"><span id="date_932011">17:00</span></td>
<td width="50"><img id="alerte_but_932011" src="/images/livescore/alerte_but.png" hspace="4" style="display:none;"></td>
<td align="center" width="160"><span id="joueur1_932011">Evian</span><input type="hidden" id="equipe1_932011" value="35533"></td>
<td align="center" width="160">
<div id="contour_score_932011" onclick="return affiche_table(932011);" style="cursor:pointer;">
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr align="center">
<td width="100%"><b><font style="color:#000;font-size:13px;"><span id="score_932011"></span></font></b></td>
<td width="15"><span id="voir_buteurs_cartons_932011" style="display:none;"><img src="/images/livescore/plus_detail.png" hspace="3" border="0"></span></td>
</tr></tbody></table>
</div>
<span id="score2_932011"><b><table cellspacing="4" cellpadding="2"><tbody><tr align="center">
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">3.71</a></td>
<td class="contour" width="20"><a class="cotes" href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')">3.25</a></td>
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">2.01</a></td>
</tr></tbody></table></b></span>
</td>
<td align="center" width="160"><span id="joueur2_932011">Lille</span></td>
<td align="center" id="td_micro_932011"><img src="/images/livescore/micro.gif"></td>
<td><a href="/evian-lille-endirect-mf932011.htm"><img id="lien_infos_932011" src="/images/livescore/infos.png" border="0" align="absmiddle" alt="Resultat match Evian Lille en direct"></a></td>
<td></td><td><a href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')"><img src="/images/pariez.png" align="absmiddle" border="0" alt="Parier"></a></td>
</tr>
<tr id="ligne2_932011" height="2" class="l2">
<td colspan="10">
<div id="extra_932011" style="display:none;" align="center">
<table cellspacing="0" cellpadding="3" style="margin-right:45px;"><tbody><tr><td width="160" valign="top"><div id="buteur1_932011"></div></td><td width="70"></td><td width="145" valign="top"><div id="buteur2_932011"></div></td></tr>
<tr><td width="160" valign="top"><div id="carton1_932011"></div></td><td width="70"></td><td width="145" valign="top"><div id="carton2_932011"></div></td></tr>
</tbody></table>
</div></td></tr><tr id="ligne_932021" class="l1">
<td onclick="return cache(932021, 2);"><img src="/images/livescore/moins_petit.gif"></td>
<td width="50"><span id="date_932021">21:00</span></td>
<td width="50"><img id="alerte_but_932021" src="/images/livescore/alerte_but.png" hspace="4" style="display:none;"></td>
<td align="center" width="160"><span id="joueur1_932021">St Etienne</span><input type="hidden" id="equipe1_932021" value="15449"></td>
<td align="center" width="160">
<div id="contour_score_932021" onclick="return affiche_table(932021);" style="cursor:pointer;">
<table cellspacing="0" cellpadding="0" width="100%"><tbody><tr align="center">
<td width="100%"><b><font style="color:#000;font-size:13px;"><span id="score_932021"></span></font></b></td>
<td width="15"><span id="voir_buteurs_cartons_932021" style="display:none;"><img src="/images/livescore/plus_detail.png" hspace="3" border="0"></span></td>
</tr></tbody></table>
</div>
<span id="score2_932021"><b><table cellspacing="4" cellpadding="2"><tbody><tr align="center">
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">2.01</a></td>
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">3.21</a></td>
<td class="contour" width="20"><a class="cotes" href="http://media.eurosportbet.com/redirect.aspx?pid=2254&amp;bid=1783" target="_blank" rel="nofollow" onclick="xt_med('C','','Unibet','S')">3.71</a></td>
</tr></tbody></table></b></span>
</td>
<td align="center" width="160"><span id="joueur2_932021">Marseille</span></td>
<td align="center" id="td_micro_932021"><img src="/images/livescore/micro.gif"></td>
<td><a href="/st-etienne-marseille-endirect-mf932021.htm"><img id="lien_infos_932021" src="/images/livescore/infos.png" border="0" align="absmiddle" alt="Resultat match St Etienne Marseille en direct"></a></td>
<td></td><td><a href="http://www.betclic.fr/partner.aspx?p=SPR&amp;z=c01" target="_blank" rel="nofollow" onclick="xt_med('C','','Betclic live','S')"><img src="/images/pariez.png" align="absmiddle" border="0" alt="Parier"></a></td>
</tr>
<tr id="ligne2_932021" height="2" class="l1">
<td colspan="10">
<div id="extra_932021" style="display:none;" align="center">
<table cellspacing="0" cellpadding="3" style="margin-right:45px;"><tbody><tr><td width="160" valign="top"><div id="buteur1_932021"></div></td><td width="70"></td><td width="145" valign="top"><div id="buteur2_932021"></div></td></tr>
<tr><td width="160" valign="top"><div id="carton1_932021"></div></td><td width="70"></td><td width="145" valign="top"><div id="carton2_932021"></div></td></tr>
</tbody></table>
</div></td></tr></tbody></table>
</body>
</html>

EOT;

$matches = array();
if(preg_match_all('#(<table\sid="table_.*?>(.*?)</table>)#is', $html, $matches)) {
print_r($matches);
}

?>