1

What's the problem of this code? I don't get it. This is the error code:

Notice: Array to string conversion in C:\xampp\htdocs\stage\ripper.php on line 12 Array Blockquote

Notice: Array to string conversion in C:\xampp\htdocs\stage\ripper.php on line 13 Array

<?php
header('Content-Type: text/html; charset=utf-8');

$url = "http://www.asaphshop.nl/epages/asaphnl.sf/nl_NL/ObjectPath=/Shops/asaphnl/Products/80203122";
$htmlcode = file_get_contents($url);
$pattern = "/itemprop=\"description\"\>(.*)\<\/div\>(.*)\<li\>Taal:(.*)\<\/li\>(.*)\>(.*)\<\/div\>\<li\>(.*)\data-src-l\<\/li\>/sU";
preg_match_all($pattern, $htmlcode, $matches);
Print_r ($matches);
$description =($matches[1]);
$language = ($matches[3]);
echo $description;
echo $language
?>
Barmar
  • 596,455
  • 48
  • 393
  • 495
bananaman
  • 31
  • 1
  • 9
  • 1
    What are the brackets doing around the operands in lines 12 & 13? – arkascha Oct 15 '14 at 07:42
  • [Parsing HTML with regex is a hard job](https://stackoverflow.com/a/4234491/372239) HTML and regex are not good friends. Use a parser, it is simpler, faster and much more maintainable. See: http://php.net/manual/en/class.domdocument.php – Toto Dec 07 '19 at 11:37

2 Answers2

4

When you use preg_match_all, $matches is a 2-dimensional array. So $matches[1] and $matches[3] are both arrays. echo only works with numbers or strings, so you get a warning when you try to echo an array. If you want to see what's in them, use print_r() or var_dump():

print_r($description);
print_r($language);
Barmar
  • 596,455
  • 48
  • 393
  • 495
  • thanks for the quick reply! but all my arrays are empty while there should be a lot of text inside them. how do i solve this? – bananaman Oct 15 '14 at 08:20
  • 1
    If they're empty it's because those parts of the regexp aren't matching anything. – Barmar Oct 15 '14 at 08:23
0

It seems $language is an array. So to print a array you either loop through it and echo each element, or you can use print_r. Other than that you can use var_dump($language) which will tell you what type it is and what it's content is . Or you can go for json_encode($language).

Hope it helps :)