2011-04-02 57 views
3

我做了一個腳本,它創建了一個從頁面上抓取的url的數組,我想過濾一個只有一個url的數組。 數組目前看起來是這樣的:PHP過濾1個網址的數組

Array 
(
    [0] => index.jsp 
    [1] => feedback.jsp 
    [2] => faq.jsp 
    [3] => donate.jsp 
    [4] => contact.jsp 
    [5] => widgetmaker.jsp 
    [11] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php 
    [12] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php 
    [13] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php 
    [14] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php 
    [15] => http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php 
) 

而我想要它做的就是抓住了「http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q & _tgt_url =之一http%3A%2F%2Fanothersite.com%2Fxml.php「鏈接。我該怎麼做呢?

回答

2

假如我理解正確的話,你想只有完全合格的(絕對)網址:如果你想同時httphttps網址

$filtered = array_filter($urls, function($url) { 
    if (strpos($url, 'http://') === 0) return true; 
    return false; 
}); 

$filtered = array_filter($urls, function($url) { 
    if (preg_match('#^https?://#', $url)) return true; 
    return false; 
}); 

如果你只是想確切匹配:

$filtered = array_filter($urls, function($url) { 
    if ($url == 'http://full/url/goes/here') return true; 
    return false; 
}); 

如果您只想獲得第一個:

$url = $filtered[0]; 
+0

我要搶「http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F% 2Fanothersite.com%2Fxml.php「鏈接 – Kraffs 2011-04-02 19:21:00

0

我認爲這個理想可以改進腳本來捕捉一個鏈接。你知道應該是最終URL的標準嗎?

恕我直言,理想情況下,使用regular expression或,如果可能的話,找到具有更高效的strpos的特定字符串。

+0

我想抓住」http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php「鏈接,但問題是,令牌不同所以我不知道如何使用正則表達式。 – Kraffs 2011-04-02 19:21:32

0

如果我正確理解你,你要麼得到的URL - 如果它存在於數組中 - 或NULL。這PHP代碼將做到這一點:

function get_url_if_present($wanted, $array) { 
    return array_keys($array, $wanted) ? $wanted : NULL; 
} 

...其中$wanted是你正在尋找在$array的URL,返回值是與發現的URL字符串,如果它存在數組中,否則NULL

你可以調用這個函數是這樣的:

<?php 

function get_url_if_present($wanted, $array) { 
    return array_keys($array, $wanted) ? $wanted : NULL; 
} 

$arr = Array 
(
    0 => "index.jsp", 
    1 => "feedback.jsp", 
    2 => "faq.jsp", 
    3 => "donate.jsp", 
    4 => "contact.jsp", 
    5 => "widgetmaker.jsp", 
    11 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php", 
    12 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php", 
    13 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php", 
    14 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php", 
    15 => "http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php" 
); 

$url_as_string = get_url_if_present("http://www.example.com/myaccount/accountactivation?_mska_tok=tON-3yIN1n5TVrFAXT3Q&_tgt_url=http%3A%2F%2Fanothersite.com%2Fxml.php", $arr); 
print $url_as_string; 

?> 
+0

我想獲得想要的網址到一個字符串,也令牌不是靜態的。 – Kraffs 2011-04-02 20:42:06

+0

好吧,我會編輯它使其成爲一個功能。 – 2011-04-02 20:45:39

+0

如果_mska_tok是靜態的,那會很好,但我擔心它的動態。你將如何獲得包含「http://www.example.com/myaccount/accountactivation」的鏈接? – Kraffs 2011-04-03 01:18:57