从表单中解析多个 url

发布于 2024-12-11 15:27:35 字数 1987 浏览 0 评论 0原文

我正在尝试制作一个脚本,该脚本在表单中给出的网址列表中搜索电子邮件地址。有人可以建议我该怎么做吗?有 cURL 的替代方案吗? 我尝试使用 file_get_contents 来实现,但脚本仅分析表单中给出的最后一个 url:例如,当我在表单中输入两个 url 时,第一个“print_r("show current_url:". $current_url); 为空并且第二个它显示页面(url)内容(没有图片)。

您的帮助。

我在不同的论坛上询问,但没有收到答案,非常感谢

$urls = explode("\n", $_POST['urls']); 

$db = new mysqli('localhost', 'root', 'root', 'urls');  

if (mysqli_connect_errno()) {
    echo 'Błąd: ';
    exit;
}


for ($i=0; $i<count($urls); $i++){

print_r("show link:". $urls[$i]."<br>");

$current_url = file_get_contents($urls[$i]);
print_r("show current_url:". $current_url); 


    preg_match( "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $current_url, $email);//email


    print_r ("show email:".$email[0]);


    $query = "INSERT INTO urle set adres = '$email[0]' "; 

    $result = $db->query($query);




}

if ($query) {
    echo $db->affected_rows ."pozycji dodano.";
} else {
    echo mysql_errno() . ":" . mysql_error() .   "Wyst&#261;pi&#322; b&#322;&#261;d przy dodawaniu urli  ";
}

$db->close();

?>

编辑: 我尝试过使用curl。 var_dump($电子邮件);显示:数组(0) { } 现在,该脚本会在浏览器中显示表单中给出的所有 url,但 preg_match 不起作用,因此它不会提取电子邮件地址。

<?php


$urls = explode("\n", $_POST['urls']); 

$db = new mysqli('localhost', 'root', 'root', 'linki');  

if (mysqli_connect_errno()) {
    echo 'Błąd: ';
    exit;
}


for ($i=0; $i<count($urls); $i++){

    $url = $urls[$i];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);

preg_match( "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $output, $email);//email
var_dump($email);

    $query = "INSERT INTO urle set adres = '$email[0]' "; 
    $result = $db->query($query);


curl_close($ch);



}//

if ($result) {
    echo $db->affected_rows ."pozycji dodano.";
} else {
    echo mysql_errno() . ":" . mysql_error() .   "Wystąpił błąd przy dodawaniu urli  ";
}

$db->close();

?>  

I'm trying to make a script, which search the list of urls given in the form for the email adresses. Could anyone advice me how to do it? Is there some alternative to cURL?
I tried to make it with file_get_contents, but the script analyze only the last url given in the form: when I enter for example two urls to the form, the first "print_r("show current_url:". $current_url); is empty and for the second it shows the page(url) content(without pictures).

I asked on different forums, but received no answer. Will really appraciate your help.

Thank you

$urls = explode("\n", $_POST['urls']); 

$db = new mysqli('localhost', 'root', 'root', 'urls');  

if (mysqli_connect_errno()) {
    echo 'Błąd: ';
    exit;
}


for ($i=0; $i<count($urls); $i++){

print_r("show link:". $urls[$i]."<br>");

$current_url = file_get_contents($urls[$i]);
print_r("show current_url:". $current_url); 


    preg_match( "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $current_url, $email);//email


    print_r ("show email:".$email[0]);


    $query = "INSERT INTO urle set adres = '$email[0]' "; 

    $result = $db->query($query);




}

if ($query) {
    echo $db->affected_rows ."pozycji dodano.";
} else {
    echo mysql_errno() . ":" . mysql_error() .   "Wystąpił błąd przy dodawaniu urli  ";
}

$db->close();

?>

EDIT:
I have tried with curl. var_dump($email); shows: array(0) { }
The script displays now all of the urls given in the form in the browser, but preg_match doesn't work, so it doesn't extract email adresses.

<?php


$urls = explode("\n", $_POST['urls']); 

$db = new mysqli('localhost', 'root', 'root', 'linki');  

if (mysqli_connect_errno()) {
    echo 'Błąd: ';
    exit;
}


for ($i=0; $i<count($urls); $i++){

    $url = $urls[$i];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
$output = curl_exec($ch);

preg_match( "/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $output, $email);//email
var_dump($email);

    $query = "INSERT INTO urle set adres = '$email[0]' "; 
    $result = $db->query($query);


curl_close($ch);



}//

if ($result) {
    echo $db->affected_rows ."pozycji dodano.";
} else {
    echo mysql_errno() . ":" . mysql_error() .   "Wystąpił błąd przy dodawaniu urli  ";
}

$db->close();

?>  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如果没结果 2024-12-18 15:27:35

有 cURL 的替代品吗?

file_get_contents,它不会给您任何错误消息(除非引发 error_reporting),除非设置了 ini_set("user_agent", ...),否则通常会被阻止。

或者,在较新的 PHP 安装上使用 HttpRequest

不过 curl 使用起来并不困难。该手册充满了示例。

第一个“print_r("show current_url:".$current_url);为空

没有人能告诉你。调试它是你的责任(特别是因为你没有在问题中提到受影响的url)。使用curl或httprequest。

Is there some alternative to cURL?

file_get_contents, which doesn't give you any error messages (unless error_reporting is raised), and which is often blocked unless ini_set("user_agent", ...) was set.

Alternatively HttpRequest on newer PHP installations.

Still curl is not difficult to use. The manual is full of examples.

the first "print_r("show current_url:". $current_url); is empty

Nobody can tell. It's your duty to debug that (especially since you haven't mentioned the affected url in your question). Use curl or httprequest.

孤檠 2024-12-18 15:27:35

好的,我已经修好了!:)

这是代码:

for ($i=0; $i<count($linki); $i++){

$url = $linki[$i];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result =curl_exec($ch);
curl_close($ch);

preg_match("/[-a-z0-9\._]+@[-a-z0-9\._]+\.[a-z]{2,4}/", $result, $email);//email
print_r($email);

    $zapytanie = "INSERT INTO urle set adres = '$email[0]' "; 
    $wynik = $db->query($zapytanie);





}

Ok, i've fixed it!!!:)

Here is the code:

for ($i=0; $i<count($linki); $i++){

$url = $linki[$i];
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$result =curl_exec($ch);
curl_close($ch);

preg_match("/[-a-z0-9\._]+@[-a-z0-9\._]+\.[a-z]{2,4}/", $result, $email);//email
print_r($email);

    $zapytanie = "INSERT INTO urle set adres = '$email[0]' "; 
    $wynik = $db->query($zapytanie);





}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文