2015-11-25 80 views
0

我目前正在开发一个足球俱乐部的网站。 我选择了官方网站上的排名(我有权限)。问题是没有RSS提要,那么我不得不修补。 我创建一个PHP文件与此代码:如何在Drupal中使用preg_match_all

<?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; 


    } 


?> 

结果:

enter image description here

回答

2

使用串连我定义如下:

$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 
+0

它的工作原理! thx :-D – user3592221

+0

如何链接此特定模块的css文件? – user3592221

0

你可以使用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

+0

它的工作原理,但Drupal的模块中没有的功能:/ – user3592221

+0

你说的不工作呢?它是否显示错误? – Daniel

+0

代码的工作原理,但不是在一个drupal页面,像一个基本的PHP文件的空白页面 – user3592221