-2
$site=file_get_contents("$link");

$price='#<span class=price>(.*?)<\/span>#si';

preg_match_all($price,$site,$pricelist);

echo $pricelist[0]."<br>";

echo $pricelist[1]."<br>";

echo $pricelist[2]."<br>";

Hello all, i am new in PHP. i am trying to take price list from another website. I tried to write this code and it said "Notice: Array to string conversion in". How can i take this prices to the list like :

<tr>
   <td><?php echo $pricelist[0] ?></td>
   <td><?php echo $pricelist[1] ?></td>
   <td><?php echo $pricelist[2] ?></td>
   <td><?php echo $pricelist[3] ?></td>
   <td><?php echo $pricelist[3] ?></td>
</tr>

And also this code worked :

<?php

$site=file_get_contents("$link");

$price='#<span class=price>(.*?)<\/span>#si';

preg_match_all($price,$site,$pricelist);

for ($a=0; $a<5; $a++){

echo $pricelist[1][$a].'<br>'; }

?>
hakre
  • 193,403
  • 52
  • 435
  • 836
Samet
  • 27
  • 2
  • 3
    **Don't use regular expressions to parse HTML**. You cannot reliably parse HTML with regular expressions. As soon as the HTML changes from your expectations, your code will be broken. See http://htmlparsing.com/php.html for examples of how to properly parse HTML with PHP modules. – Andy Lester Jan 07 '13 at 18:45
  • whats the problem to it in for loop since you get multidimensional array – v0d1ch Jan 07 '13 at 18:46
  • 1
    Notice how you're using `$pricelist[0]` in the not-working code, and `$pricelist[1][$a]` in the *working* code? – Sammitch Jan 07 '13 at 18:47
  • [*"HTML is not a regular language and hence cannot be parsed by regular expressions."*](http://stackoverflow.com/a/1732454/265575) – Justin ᚅᚔᚈᚄᚒᚔ Jan 07 '13 at 18:51

2 Answers2

0

I guess you want this--

$site=file_get_contents("$link");

$price='#<span class=price>(.*?)<\/span>#si';

preg_match_all($price,$site,$pricelist);

echo "<tr>";

for ($a=0; $a<5; $a++){

echo "<td>".$pricelist[1][$a].'</td>'; 
}
echo "</tr>";
Suresh Kamrushi
  • 15,627
  • 13
  • 75
  • 90
0

$pricelist[0] (or any index for that matter) is apparently an array. This is confirmed by the code you posted that worked. So you need to determine how you want to output that array. A simple solution would be to use print_r

<tr>
   <td><?php print_r($pricelist[0]); ?></td>
   <td><?php print_r($pricelist[1]); ?></td>
   <td><?php print_r($pricelist[2]); ?></td>
   <td><?php print_r($pricelist[3]); ?></td>
   <td><?php print_r($pricelist[3]); ?></td>
</tr>
Matt Dodge
  • 10,833
  • 7
  • 38
  • 58