繁体   English   中英

PHP搜索URL源代码的特定单词,然后重定向到URL

[英]php search url source code for specific word and then redirect to url

我需要php代码来搜索特定词的url源代码,如果该词存在,它将重定向到该url。 我有以下代码,但我不知道如何执行重定向部分:

<?php
$ch =  curl_init("http://www.example.com");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);

echo (stristr ($result, 'specificword')) ? "<div style='text-align:center; color:green'>Online</div>" : "<div style='text-align:center; color:red'>Offline</div>";
?>

UPDATE :使用您提供的代码,这是我的解决方法:

<?php
    $URL = 'http://www.example.com';
    $ch =  curl_init($URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    if (stristr ($result, 'Domain')) {
        header('Location: '. $URL);
    }
    else{
        echo "No matches found";
    }
?>

旧答案

如果首先要显示ONLINE ,则需要META刷新之类的东西。

将最后一行更改为:

echo (CONDITION HERE) '<html>
<head>
<title>Site online</title>
<meta http-equiv="refresh" content="5; url=http://example.com/">
</head>
<body>
<div style="text-align:center; color:green">Online</div>" : "<div style='text-align:center; color:red'>Offline</div>
</body>
</html>';

如果您不需要先显示ONLINE而只想重定向,请尝试:

if (stristr ($result, 'specificword')) {
  header('Location: http://www.example.com');
}

我也建议使用mb_stristr因为它可以更好地处理Unicode。

我更新的代码如下:

<?php
    $URL = 'http://www.example.com';
    $ch =  curl_init($URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $result = curl_exec($ch);

    if (stristr ($result, 'Domain')) {
        header('Location: '. $URL);
    }
    else{
        echo "No matches found";
    }
?>
if something == true then
     header("location: myurl.com");
     exit;
end

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM