Search code examples
phparraysstringpreg-match-all

Array to string conversion


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

Solution

  • 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);