Arw*_*wed 1 php arrays function
函数内部的代码正在运行,但由于我想要处理多个Url,我想让它成为一个函数,使用数组来获取要处理的URL.这是代码:
<?php
$site = array("http://www.ihr-apotheker.de/cs1.html", "http://www.ihr-apotheker.de/cs2.html", "http://www.ihr-apotheker.de/cs3.html");
function parseNAI($sites)
{
foreach ($sites as $html)
{
$clean_one = strstr($html, '<p>');
$clean_one_class = str_replace('<p><span class="headline">', '<p class="headline gruen"><span>', $clean_one);
$clean_one_class_a = strip_tags($clean_one_class, '<p><span><a>');
$clean_one_class_b = preg_replace("/\s+/", " ", $clean_one_class_a);
$str_one = preg_replace('#(<a.*>).*?(</a>)#', '$1$2', $clean_one_class_b);
$ausgabe_one = strip_tags($str_one, '<p>');
echo $ausgabe_one;
}
};
parseNAI($site);
?>
Run Code Online (Sandbox Code Playgroud)
我的问题在哪里,因为该函数在foreach开始时停止工作....请提前帮助你!
我有一种感觉,你错过了那里的一步...也许是一个file_get_contents或类似的?现在你在uri上运行一堆字符串函数,而不是uri的源代码.
试试这个:
<?php
$site = array("http://www.ihr-apotheker.de/cs1.html", "http://www.ihr-apotheker.de/cs2.html", "http://www.ihr-apotheker.de/cs3.html");
function parseNAI($sites)
{
foreach ($sites as $url)
{
$html = file_get_contents($url);
$clean_one = strstr($html, '<p>');
$clean_one_class = str_replace('<p><span class="headline">', '<p class="headline gruen"><span>', $clean_one);
$clean_one_class_a = strip_tags($clean_one_class, '<p><span><a>');
$clean_one_class_b = preg_replace("/\s+/", " ", $clean_one_class_a);
$str_one = preg_replace('#(<a.*>).*?(</a>)#', '$1$2', $clean_one_class_b);
$ausgabe_one = strip_tags($str_one, '<p>');
echo $ausgabe_one;
}
};
parseNAI($site);
?>
Run Code Online (Sandbox Code Playgroud)