Php 在foreach循环中声明函数
我正在尝试创建一个foreach循环,它将从一系列链接中访问一个链接,并从每个访问过的网站中获取内容 但当我尝试运行它时:Php 在foreach循环中声明函数,php,dom,Php,Dom,我正在尝试创建一个foreach循环,它将从一系列链接中访问一个链接,并从每个访问过的网站中获取内容 但当我尝试运行它时: <?php require('simple_html_dom.php'); // Create DOM from URL or file $html = file_get_html('http://www.forgolf.cz/index.php/novinky.html'); $titles = []; $links = [
<?php
require('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.forgolf.cz/index.php/novinky.html');
$titles = [];
$links = [];
$img_src = [];
$paragraph = [];
foreach ($html->find('h2 a') as $title) {
$links[] = $title->getAttribute('href');
$titles[] = $title->innertext;
}
foreach ($html->find('.item-inner p img') as $img) {
$img_src[] = $img->getAttribute('src');
}
foreach ($links as $link) {
require('simple_html_dom.php');
$html = file_get_html('http://www.forgolf.cz'. $link);
foreach ($html-find('.item-page p') as $p) {
if (is_string($p))
$paragraph[] = $p->innertext;
}
}
?>
I get致命错误:无法重新声明文件\u get\u html()
当我不包括require('simple_html_dom.php')代码>
我得到:致命错误:未捕获错误:调用未定义的函数find()
所以我真的很困惑
有人知道如何修理这个吗。改变
require('simple_html_dom.php');
到
在调用此函数的所有实例中,不仅仅是在内部循环中。请更改代码行
foreach ($links as $link) {
require('simple_html_dom.php'); **// here** comment line like **//require('simple_html_dom.php');**
$html = file_get_html('http://www.forgolf.cz'. $link);
foreach ($html-find('.item-page p') as $p) {
if (is_string($p))
$paragraph[] = $p->innertext;
}
}
?>
最后编码
<?php
require ('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.forgolf.cz/index.php/novinky.html');
$titles = [];
$links = [];
$img_src = [];
$paragraph = [];
foreach($html->find('h2 a') as $title)
{
$links[] = $title->getAttribute('href');
$titles[] = $title->innertext;
}
foreach($html->find('.item-inner p img') as $img)
{
$img_src[] = $img->getAttribute('src');
}
foreach($links as $link)
{
// require('simple_html_dom.php');
$html = file_get_html('http://www.forgolf.cz' . $link);
foreach($html->find('.item-page p') as $p)
{
if (is_string($p)) $paragraph[] = $p->innertext;
}
}
?>
在这段代码中,你从来没有声明过文件\u get\u html()
,你只需要使用它。但是如果(!function_存在(“file_get_html”){/*declare function here*/}
围绕它,您就不会再收到错误了。不过,在循环中声明函数听起来像是一个糟糕的设计。您要删除的函数是,仅在循环中声明函数还是在循环中声明函数以上?我对这两个实例都更改了require\u一次,但仍然得到:致命错误:未捕获错误:调用未定义的函数find()
我尝试了您的解决方案,但是当我尝试var\u dump($paration)
I得到空数组:/使用这个codefeach($links作为$link){//require('simple\u html\u dom.php');$html=file\u get\u html('.$link);foreach($html->find('.item page p')作为$p){$paragration[]=$p->innertext;}}var\u dump($paration); ?>这里是代码
<?php
require ('simple_html_dom.php');
// Create DOM from URL or file
$html = file_get_html('http://www.forgolf.cz/index.php/novinky.html');
$titles = [];
$links = [];
$img_src = [];
$paragraph = [];
foreach($html->find('h2 a') as $title)
{
$links[] = $title->getAttribute('href');
$titles[] = $title->innertext;
}
foreach($html->find('.item-inner p img') as $img)
{
$img_src[] = $img->getAttribute('src');
}
foreach($links as $link)
{
// require('simple_html_dom.php');
$html = file_get_html('http://www.forgolf.cz' . $link);
foreach($html->find('.item-page p') as $p)
{
if (is_string($p)) $paragraph[] = $p->innertext;
}
}
?>