Do you see anything wrong with this foreach loop ?
If not, then why I get error ?
I got 4 foreach loops. First two are ok. Last two causing this same type of error.
Following is my third foreach loop where I get error.
[code]
if(ISSET($ignored_links))
{
$ignored_links_count = count($ignored_links);
echo ‘Ignored Links Count: ‘ .$ignored_links_count; echo ‘<br>’;
}
echo ‘Ignored Links: ‘; echo ‘<br>’;
if($ignored_links_count>=1)
{
//On this Crawling S ssion, atleast one Scraped Link has been discarded.
foreach($ignored_links as $ignored_link)
{
//Display a List of all Discarded Links.
echo __LINE__; echo ‘<br>’;
echo $ignored_link; echo ‘<br>’; //”Notice Array to String Conversion” error on this line.
echo ‘<br>’;
}
}
[code]
<i>
</i>if(ISSET($harvested_links))
{
$harvested_links_count = count($harvested_links);
echo 'Harvested Links Count: ' .$harvested_links_count; echo '<br>';
}
echo 'Harvested Links: '; echo '<br>';
if($harvested_links_count>=1)
{
//On this Crawling Session, atleast one Link has been Scraped and Indexed.
foreach($harvested_links as $harvested_link)
{
//Display a List of all Indexed Links.
echo __LINE__; echo '<br>';
echo $harvested_link; echo '<br>'; //"Notice Array to String Conversion" error on this line.
echo '<br>';
}
}
}
<i>
</i><?php
require('Conn_Template.php');
include('Error_Reporting_Template.php');
include('simplehtmldom_1_9_1/simple_html_dom.php');
?>
<!DOCTYPE html>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset>
<label for="url">Url:</label>
<input type="text" name="url" id="url" value="<?php if(ISSET($_POST['url'])) { echo $_POST['url']; }elseif(ISSET($_GET['url'])) { echo $_GET['url']; } ?>" required>
<br>
</fieldset>
<fieldset>
<input type="submit" name="fetch" id="fetch" value="Fetch">
</fieldset>
</form>
<?php
$links_to_visit = array();
$visited_links = array();
$scraped_links = array();
$harvested_links[] = array();
$ignored_links[] = array();
if($_SERVER['REQUEST_METHOD']=="POST")
{
//Check if URL has been input or not
if(!ISSET($_POST['url']))
{
die('Error 1: Internal Error!');
}
elseif($_POST['url']=='')
{
die('Error 2: Input Ur!');
}
elseif(ISSET($_POST['url']) && $_POST['url']!='');
{
$url = $_POST['url'];
$links_to_visit[0] = $url;
}
}
//Visit the Submitted Link plus all links scraped.j
foreach($links_to_visit as $link_to_visit)
{
$html = file_get_html("$link_to_visit");
$visited_links[] = $link_to_visit;
<i> </i>//Scrape all Links found in the Visited Page.
<i> </i>foreach($html->find('a') as $element)
<i> </i>{
<i> </i> $scraped_links[] = $element;
<i> </i> echo __LINE__; echo '<br>';
<i> </i>}
<i> </i>
<i> </i>//Check if Scraped Link already scraped before or not.
<i> </i>foreach($scraped_links as $scraped_link)
<i> </i>{
<i> </i> if(!in_array($scraped_link,$visited_links) && (!in_array($scraped_link,$links_to_visit)))
<i> </i> {
<i> </i> //Scraped Link is new.
<i> </i> $links_to_visit[] = $scraped_link;
<i> </i> $harvested_links[] = $scraped_link;
<i> </i> }
<i> </i> elseif(in_array($scraped_link,$visited_links) && (!in_array($scraped_link,$links_to_visit)))
<i> </i> {
<i> </i> //Scraped Link already scraped before.
<i> </i> $ignored_links[] = $scraped_link;
<i> </i> }
<i> </i>
<i> </i> //Add current Visited Page to "Visited Links" list to avoid any further visit to it.
<i> </i> $visited_links[] = $link_to_visit;
<i> </i> echo __LINE__; echo '<br>';
<i> </i>}
<i> </i>
<i> </i>//Display Stats from this point ohnwards
<i> </i>if(ISSET($visited_links))
<i> </i>{
<i> </i> $visited_links_count = count($visited_links); echo '<br>';
<i> </i> echo 'Visited Links: '; echo '<br>';
<i> </i>}
<i> </i>if($visited_links_count>=1)
<i> </i>{
<i> </i> //On this Crawling Session, atleast one link has been crawled.
<i> </i> foreach($visited_links as $visited_link)
<i> </i> {
<i> </i> //Disay a List of all Crawled Links
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $visited_link; echo '<br>';
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
<i> </i>
<i> </i>if(ISSET($scraped_links))
<i> </i>{
<i> </i> $scraped_links_count = count($scraped_links);
<i> </i> echo 'Found Links Count: ' .$scraped_links_count; echo '<br>';
<i> </i>}
<i> </i>echo 'Found Links: '; echo '<br>';
<i> </i>if($scraped_links_count>=1)
<i> </i>{
<i> </i> //On this Crawling Session, atleast one Link has been Scraped.
<i> </i> foreach($scraped_links as $scraped_link)
<i> </i> {
<i> </i> //Display a List of all Scraped Links.
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $scraped_link; echo '<br>';
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
<i> </i>
<i> </i>if(ISSET($ignored_links))
<i> </i>{
<i> </i> $ignored_links_count = count($ignored_links);
<i> </i> echo 'Ignored Links Count: ' .$ignored_links_count; echo '<br>';
<i> </i>}
<i> </i>echo 'Ignored Links: '; echo '<br>';
<i> </i>if($ignored_links_count>=1)
<i> </i>{
<i> </i> //On this Crawling S ssion, atleast one Scraped Link has been discarded.
<i> </i> foreach($ignored_links as $ignored_link)
<i> </i> {
<i> </i> //Display a List of all Discarded Links.
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $ignored_link; echo '<br>'; //"Notice Array to String Conversion" error on this line.
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
<i> </i>
<i> </i>if(ISSET($harvested_links))
<i> </i>{
<i> </i> $harvested_links_count = count($harvested_links);
<i> </i> echo 'Harvested Links Count: ' .$harvested_links_count; echo '<br>';
<i> </i>}
<i> </i>echo 'Harvested Links: '; echo '<br>';
<i> </i>if($harvested_links_count>=1)
<i> </i>{
<i> </i> //On this Crawling Session, atleast one Link has been Scraped and Indexed.
<i> </i> foreach($harvested_links as $harvested_link)
<i> </i> {
<i> </i> //Display a List of all Indexed Links.
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $harvested_link; echo '<br>'; //"Notice Array to String Conversion" error on this line.
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
}
/*
var_dump($ignored_link);
echo '<br>';
var_dump($harvested_link);
echo '<br>';
var_dump($ignored_links);
echo '<br>';
var_dump($harvested_links);
*/
?>
>@developer_web#1628010 Both $ignored_links and $harvested_links are arrays.
> Hence, I never should've got those errors.
echo
echo
string
> Description
>
>echo ( string $arg , string ...$args ) : void
<i>
</i>foreach($harvested_links as $harvested_link)
{
echo $harvested_link;
}
<i>
</i>foreach($array as $value)
{
if(strlen($value)!=0)
echo $value:
}
<i>
</i>foreach($array as $value)
{
if(strlen($value)>1)
echo $value:
}
<i>
</i>foreach($array as $value)
{
if($value != "")
echo $value:
}
<i>
</i>foreach($array as $value)
{
if($value)=='NULL')
echo $value:
}
<i>
</i>foreach($array as $value)
{
if(!empty($value))
echo $value:
}
<i>
</i>foreach($array as $value)
{
if($value))
echo $value:
}
<i>
</i>foreach($array as $value)
{
if(strlen($value)!==0)
echo $value:
}
<i>
</i>foreach($array as $value)
{
if($value) != 'NULL')
echo $value:
}
<i>
</i>foreach($ignored_links as $ignored_link)
{
if($ignored_link != '')
{
echo 'Ignored Links: ' .$ignored_link .'<br>';
}
}
<i>
</i>$harvested_links[] = array();
$ignored_links[] = array();
>Note the singular and plural here.
$harvested_links[] = array();
</C><br/>
creates a two dimensional array where <C>
$harvested_links[0]
`<i>
</i><?php
require('Conn_Template.php');
include('Error_Reporting_Template.php');
include('simplehtmldom_1_9_1/simple_html_dom.php');
?>
<!DOCTYPE html>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
<fieldset>
<label for="url">Url:</label>
<input type="text" name="url" id="url" value="<?php if(ISSET($_POST['url'])) { echo $_POST['url']; }elseif(ISSET($_GET['url'])) { echo $_GET['url']; } ?>" required>
<br>
</fieldset>
<fieldset>
<input type="submit" name="fetch" id="fetch" value="Fetch">
</fieldset>
</form>
<?php
$links_to_visit = array();
$visited_links = array();
$scraped_links = array();
$harvested_links[] = array();
$ignored_links[]= array();
if($_SERVER['REQUEST_METHOD']=="POST")
{
//Check if URL has been input or not
if(!ISSET($_POST['url']))
{
die('Error 1: Internal Error!');
}
elseif($_POST['url']=='')
{
die('Error 2: Input Ur!');
}
elseif(ISSET($_POST['url']) && $_POST['url']!='');
{
$url = $_POST['url'];
$links_to_visit[0] = $url;
}
}
//Visit the Submitted Link plus all links scraped.j
foreach($links_to_visit as $link_to_visit)
{
$html = file_get_html("$link_to_visit");
$visited_links[] = $link_to_visit;
<i> </i>//Scrape all Links found in the Visited Page.
<i> </i>foreach($html->find('a') as $element)
<i> </i>{
<i> </i> $scraped_links[] = $element;
<i> </i> echo __LINE__; echo '<br>';
<i> </i>}
<i> </i>
<i> </i>//Check if Scraped Link already scraped before or not.
<i> </i>foreach($scraped_links as $scraped_link)
<i> </i>{
<i> </i> if(!in_array($scraped_link,$visited_links) && (!in_array($scraped_link,$links_to_visit)))
<i> </i> {
<i> </i> //Scraped Link is new.
<i> </i> $links_to_visit[] = $scraped_link;
<i> </i> $harvested_links[] = $scraped_link;
<i> </i> }
<i> </i> elseif(in_array($scraped_link,$visited_links) && (!in_array($scraped_link,$links_to_visit)))
<i> </i> {
<i> </i> //Scraped Link already scraped before.
<i> </i> $ignored_links[] = $scraped_link;
<i> </i> }
<i> </i>
<i> </i> //Add current Visited Page to "Visited Links" list to avoid any further visit to it.
<i> </i> $visited_links[] = $link_to_visit;
<i> </i> echo __LINE__; echo '<br>';
<i> </i>}
<i> </i>
<i> </i>//Display Stats from this point onwards
<i> </i>if(ISSET($visited_links))
<i> </i>{
<i> </i> $visited_links_count = count($visited_links); echo '<br>';
<i> </i> echo 'Visited Links: '; echo '<br>';
<i> </i>}
<i> </i>if($visited_links_count>0)
<i> </i>{
<i> </i> //On this Crawling Session, atleast one link has been crawled.
<i> </i> foreach($visited_links as $visited_link)
<i> </i> {
<i> </i> //Disay a List of all Crawled Links
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $visited_link; echo '<br>';
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
<i> </i>
<i> </i>if(ISSET($scraped_links))
<i> </i>{
<i> </i> $scraped_links_count = count($scraped_links);
<i> </i> echo 'Found Links Count: ' .$scraped_links_count; echo '<br>';
<i> </i>}
<i> </i>echo 'Found Links: '; echo '<br>';
<i> </i>if($scraped_links_count>0)
<i> </i>{
<i> </i> //On this Crawling Session, atleast one Link has been Scraped.
<i> </i> foreach($scraped_links as $scraped_link)
<i> </i> {
<i> </i> //Display a List of all Scraped Links.
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $scraped_link; echo '<br>';
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
<i> </i>
<i> </i>if(ISSET($ignored_links))
<i> </i>{
<i> </i> $ignored_links_count = count($ignored_links);
<i> </i> echo 'Ignored Links Count: ' .$ignored_links_count; echo '<br>';
<i> </i>}
<i> </i>echo 'Ignored Links: '; echo '<br>';
<i> </i>if($ignored_links_count>0)
<i> </i>{
<i> </i> //On this Crawling S ssion, atleast one Scraped Link has been discarded.
<i> </i> foreach($ignored_links as $ignored_link)
<i> </i> {
<i> </i> //Display a List of all Discarded Links.
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $ignored_link; echo '<br>'; //"Notice Array to String Conversion" error on this line.
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
<i> </i>
<i> </i>if(ISSET($harvested_links))
<i> </i>{
<i> </i> $harvested_links_count = count($harvested_links);
<i> </i> echo 'Harvested Links Count: ' .$harvested_links_count; echo '<br>';
<i> </i>}
<i> </i>echo 'Harvested Links: '; echo '<br>';
<i> </i>if($harvested_links_count>=1)
<i> </i>{
<i> </i> //On this Crawling Session, atleast one Link has been Scraped and Indexed.
<i> </i> foreach($harvested_links as $harvested_link)
<i> </i> {
<i> </i> //Display a List of all Indexed Links.
<i> </i> echo __LINE__; echo '<br>';
<i> </i> echo $harvested_link; echo '<br>'; //"Notice Array to String Conversion" error on this line.
<i> </i> echo '<br>';
<i> </i> }
<i> </i>}
}
var_dump($ignored_link);
echo '<br>';
var_dump($harvested_link);
echo '<br>';
var_dump($ignored_links);
echo '<br>';
var_dump($harvested_links);
?>
``D:Gemeinsame DateienWebentwicklungthread588_developer_web_scraper.php:149:<i>
</i>array (size=1)
0 =>
array (size=0)
empty
D:Gemeinsame DateienWebentwicklungthread588_developer_web_scraper.php:151:
array (size=3)
0 =>
array (size=0)
empty
1 =>
object(simple_html_dom_node)[19]
public 'nodetype' => int 1
public 'tag' => string 'a' (length=1)
public 'attr' =>
array (size=1)
'href' => string 'link.html' (length=53)
public 'children' =>
array (size=0)
empty
public 'nodes' =>
... shortend here
2 =>
object(simple_html_dom_node)[23]
public 'nodetype' => int 1
public 'tag' => string 'a' (length=1)
public 'attr' =>
array (size=1)
'href' => string 'link.html' (length=97)
public 'children' =>
array (size=0)
empty
public 'nodes' =>
... shortened here<i>
</i>
`</CODE>
The first element is an empty array, entered by the faulty line<br/>
<C>
$harvested_links[] = array();
</C><br/>
The next elements are objects containing information about the a-tags. As the __toString method is implemented, when echoing such object an a-tag is output like this:<br/>
<C>
<a href="http://link.html">link text</a>
`
0.1.9 — BETA 5.20