2013-01-02 70 views
1

我之情況是:問題與PHP數組

用戶要檢查特定的(客戶)URL出現在目標網址或不和我創建簡單的腳本來測試特定的URL在一個目標網址。

這裏是我的PHP腳本:

if(isset($_POST['check_url'])) 
{ 
    $client_url = $_POST['client_url']; 
    $destination_url = $_POST['destination_url']; 
    $contents = file_get_contents($destination_url); 
    $search = $client_url; 

    if(strpos($contents,$search)== FALSE) 
    { 
     echo "Not Found"; 
    } 
    else 
    { 
     echo "Found"; 
    } 
} 

這裏是我的HTML腳本:

<form method="post" action="test.php"> 
<label>Client URL:</label> 
<input type="text" value="" name="client_url" /><br /> 
<label>Destination URL:</label> 
<textarea value="" name="destination_url" ></textarea><br /> 
<button type="submit" name="check_url">Check</button> 
</form> 

上述腳本是工作在單目標網址的情況下,但是當我嘗試發佈多個目標網址(通過將其轉換爲陣列)我得到錯誤:

Warning: file_get_contents(http://learntk12.org/story.php?title=seo-link-building-service) [function.file-get-contents]: failed to open stream: No such file or directory in "path to source file" on line 24 

其中第24行是:$contents[$i] = file_get_contents($arr[$i]);

這裏是我的PHP代碼數組:

if(isset($_POST['check_url'])) 
{ 
    $client_url = $_POST['client_url']; 
    $destination_url = $_POST['destination_url']; 
    $destination =str_replace("\r",",",$destination_url); 
    $arr = explode(",",$destination); 

    $search = $client_url; 

    for($i=0;$i<count($arr);$i++) 
    { 
     $contents[$i] = file_get_contents($arr[$i]); 

     if (strpos($contents[$i], $search) === FALSE) 
     { 
      echo "Not Found"; 
     } 
     else 
     { 
      echo "Found"; 
     } 
    } 


} 

我在哪裏,在該腳本落後?

+0

第一codelisting擁有無可匹敵的收盤'}' – Ikke

+0

謝謝,我只是糾正了 – Arish

回答

1

試試這個:

if (isset($_POST['check_url'])) 
{ 
    $client_url = $_POST['client_url']; 
    $destination_url = $_POST['destination_url']; 
    $destinations = str_replace(array("\r\n", "\n"), ',', $destination_url); // replace both types of line breaks with a comma, \r alone will not suffice 
    $destinations = explode(',', $destinations); // split at commas 

    foreach ($destinations as $destination) // loop through each item in array 
    { 
     $contents = file_get_contents($destination); // get contents of URL 

     echo (FALSE === strpos($contents, $client_url)) // check if remote data contains URL 
      ? 'Not Found' // echo if TRUE of above expression 
      : 'Found'; // echo if FALSE of above expression 
    } 
} 
+0

感謝神祕,懂了。能否請你解釋一下這個腳本是如何工作的 – Arish

+1

@Arish,耶給我一分鐘來給代碼添加註釋。 –

0

據我看到你沒有數組$內容。它應該是這樣的:

$contents = file_get_contents($arr[$i]); 
if (strpos($contents, $search) === FALSE) { 
    echo "Not Found"; 
} else { 
    echo "Found"; 
} 
+0

我得到了同樣的錯誤消息 – Arish