【发布时间】:2017-08-14 22:57:07
【问题描述】:
我想用 PHP 从this site 中检索数据并显示出来。
我使用 PHP 中的 pregmatch 函数在 3 个不同的表中检索了所需的值(从文章中的文章名称、价格以及下面的其他值)。剩下的就是将它们显示在一个二维的表格中。
表格的第一行应该有文章名称和价格。其余的行应包含标题及其值。
这是我当前的 PHP 代码:
<?php
$debut="https://www.agriconomie.com";
$txt = file_get_contents('https://www.agriconomie.com/pieces-agricoles/tracteur/attelage---relevage/pc2902'); /*ici c'est pour Lire la page html*/
$results = array();
// $test = preg_match_all('#<a href="(.*?)">#', $txt, $names_array);
$test = preg_match_all('#<a href="(.+)" class="(.+)" title="(.+)"#', $txt, $names_array);
/*recupéré les liens du site en particuliers le text qui se situe entre griffe "" du href*/
for($i = 0; $i < count($names_array[1]); $i++)
{
$j=$i;
$debut="https://www.agriconomie.com".$names_array[1][$i];
$adresse =$debut;
/* echo $adresse ; ?> <br /> <?php */
$page = file_get_contents ($adresse);
/* preg_match_all ('#<h3 class="product-name">(.+)</h3>#', $page, $names_array5); */
preg_match_all ('#(<dd>(.+)</dd>)#', $page, $names_array2);
preg_match_all ('#<span><i class="icon-chevron-right"></i>(.*?)</span>#', $page, $names_array3);
preg_match_all ('#<p class="price" itemprop="price" content="(.*?)">#', $page, $names_array4);
echo "<center>";
echo "<table class='table table-bordered table-striped table-condensed'>";
/*
for($j = 0; $j < count($names_array5[1]); $j++)
{
$NOM = $names_array5[1][$j];
echo 'Nom ='.$NOM ;
}
*/
for($j = 0; $j < count($names_array4[1]); $j++)
{
$price = $names_array4[1][$j];
echo 'Prix ='.$price.'$' ;
}
for($i = 0; $i < count($names_array3[1]); $i++)
{
for($j= 0; $j < count($names_array2[1]); $j++){
$descriptif = $names_array2[1][$i];
}
$intitule = $names_array3[1][$i];
echo "<tr><td>".$intitule." </td> <td> ".$descriptif." </td> </tr> ";
}
}
echo "</table>";
echo "</center>";
?>
【问题讨论】:
-
请在发布您的问题之前格式化您的代码。
-
如何格式化我的代码?这是我的源代码!请帮助我。
-
您的代码已被@Qirel 格式化和编辑
-
谢谢!我希望有人可以帮助我。
-
您已经描述了您需要实现的目标,但您没有告诉我们您在工作时遇到的具体问题。
标签: php regex preg-match-all