簡體   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