如何在 Drupal 中使用 preg_match_all
How to use preg_match_all in Drupal
我目前正在为一家足球俱乐部开发网站。
我选择了官方网站上的排名(我已获得许可)。问题是没有RSS feed,那我得去修修补补了。
我使用以下代码创建了一个 php 文件:
<?php
$adresse = "http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1";
$page = file_get_contents ($adresse);
preg_match_all ('#<table cellpadding="0" cellspacing="0" border="0" summary="" class="tablo bordure ac">(.*?)</table>#', $page, $prix);
// On stocke dans le tableau $prix les éléments trouvés
echo '<table>';
for($i = 0; $i < count($prix[1]); $i++) // On parcourt le tableau $prix[1]
{
echo $prix[1][$i]; // On affiche le prix
}
echo '</table>';
?>
问题是我有集成 drupal 的那个,但它不起作用。我试图创建一个模块并创建一个函数,结果相同。
我用 php 代码创建了一个内容页面,结果相同,但它不起作用。
我该怎么做?
干杯
更新:在我的 drupal 中 module.module:
<?php
include_once('simple_html_dom.php');
function classements_liste(){
$url="http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1";
$dom = str_get_html(file_get_contents($url));
$tables = $dom->find('table');
echo $tables[0];
echo $tables[1];
}
function classements_menu() {
$items['classements/list'] = array(
'title' => 'Liste des classements',
'page callback' => 'classements_liste',
'page arguments' => array('access content'),
'access callback' => true,
'type' => MENU_CALLBACK,
);
return $items;
}
?>
结果:
您可以使用 simplehtmldom 遍历 html 结构而不是 preg_match_all。
include('simple_html_dom.php');
$html = file_get_html('http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1');
$table = $html->find('table[class=\'tablo bordure ac\']', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
$row = array();
foreach($row->find('td') as $cell) {
$row[] = $cell->plaintext;
}
$rowData[] = $row;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
示例取自 here。
使用我在下面定义的串联:
$adresse = "http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1";
$page = file_get_contents ($adresse);
preg_match_all('#<table cellpadding="0" cellspacing="0" border="0" summary="" class="tablo bordure ac">(.*?)</table>#', $page, $prix);
$data = '<table>'; // here the table is define
for($i = 0; $i < count($prix[1]); $i++){
$data .='<tr><td>'.$prix[1][$i].'</td></tr>'; // add table row
}
$data .= '</table>'; // finish table
$block['content'] = $data; // assign to block or simple print in case of page
我目前正在为一家足球俱乐部开发网站。 我选择了官方网站上的排名(我已获得许可)。问题是没有RSS feed,那我得去修修补补了。 我使用以下代码创建了一个 php 文件:
<?php
$adresse = "http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1";
$page = file_get_contents ($adresse);
preg_match_all ('#<table cellpadding="0" cellspacing="0" border="0" summary="" class="tablo bordure ac">(.*?)</table>#', $page, $prix);
// On stocke dans le tableau $prix les éléments trouvés
echo '<table>';
for($i = 0; $i < count($prix[1]); $i++) // On parcourt le tableau $prix[1]
{
echo $prix[1][$i]; // On affiche le prix
}
echo '</table>';
?>
问题是我有集成 drupal 的那个,但它不起作用。我试图创建一个模块并创建一个函数,结果相同。 我用 php 代码创建了一个内容页面,结果相同,但它不起作用。
我该怎么做?
干杯
更新:在我的 drupal 中 module.module:
<?php
include_once('simple_html_dom.php');
function classements_liste(){
$url="http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1";
$dom = str_get_html(file_get_contents($url));
$tables = $dom->find('table');
echo $tables[0];
echo $tables[1];
}
function classements_menu() {
$items['classements/list'] = array(
'title' => 'Liste des classements',
'page callback' => 'classements_liste',
'page arguments' => array('access content'),
'access callback' => true,
'type' => MENU_CALLBACK,
);
return $items;
}
?>
结果:
您可以使用 simplehtmldom 遍历 html 结构而不是 preg_match_all。
include('simple_html_dom.php');
$html = file_get_html('http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1');
$table = $html->find('table[class=\'tablo bordure ac\']', 0);
$rowData = array();
foreach($table->find('tr') as $row) {
$row = array();
foreach($row->find('td') as $cell) {
$row[] = $cell->plaintext;
}
$rowData[] = $row;
}
echo '<table>';
foreach ($rowData as $row => $tr) {
echo '<tr>';
foreach ($tr as $td)
echo '<td>' . $td .'</td>';
echo '</tr>';
}
echo '</table>';
示例取自 here。
使用我在下面定义的串联:
$adresse = "http://lafa.fff.fr/competitions/php/club/club_classement_deta.php?sa_no=2011&cp_no=272284&ph_no=1&gp_no=1&cl_no=3232&eq_no=1";
$page = file_get_contents ($adresse);
preg_match_all('#<table cellpadding="0" cellspacing="0" border="0" summary="" class="tablo bordure ac">(.*?)</table>#', $page, $prix);
$data = '<table>'; // here the table is define
for($i = 0; $i < count($prix[1]); $i++){
$data .='<tr><td>'.$prix[1][$i].'</td></tr>'; // add table row
}
$data .= '</table>'; // finish table
$block['content'] = $data; // assign to block or simple print in case of page