[英]File_get_contents and Preg_match_all Empty Array
我試圖從未超鏈接的XML文本進行匹配,並將其列出為超鏈接。 但是它只顯示為數組。我在做什么錯? 謝謝
<?php
$content = file_get_contents('http://phobos.apple.com/version');
$download = preg_match_all('~(.*)/iPhone/(.*)Restore.ipsw~', $content, $matches);
echo $matches[0];
?>
轉換為鏈接的示例文本http://appldnld.apple.com/iPhone4/061-7939.20100908.Lcyg3/iPhone3,1_4.1_8B117_Restore.ipsw
如果匹配項應如下所示
根據文檔 , $matches
不是數組,而是arrays的數組 :
火柴
多維數組中所有匹配項的數組,根據標志進行排序。
默認flags
參數為PREG_PATTERN_ORDER
:
對結果進行排序,以使$ matches [0]是完整模式匹配的數組,$ matches [1]是由第一個帶括號的子模式匹配的字符串的數組,依此類推。
因此,對您而言, $matches[0]
是與您的正則表達式匹配的字符串數組 , $matches[1]
是與\\1
對應的子字符串數組 , $matches[2]
是與\\2
對應的子字符串數組 。 例如:
php >print_r($matches[2]);
Array
(
[0] => 061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_
[1] => 061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_
[2] => 061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_
[3] => 061-4313.20080226.Sw39i/iPhone1,1_1.1.4_4A102_
... etc etc
)
因此, $matches[j][i]
是第 i個匹配項的第j個捕獲組。 (其中j=0
是整個匹配字符串)。
謝謝..所以我編碼
<?php
$content = file_get_contents('http://phobos.apple.com/version');
$download = preg_match_all('~(.*)/iP(.*)Restore.ipsw~', $content, $matches, PREG_PATTERN_ORDER);
echo "Key: <br>";
print_r($matches[0]);
echo "<p>URL: <br>";
print_r($matches[1]);
?>
當我需要將文本轉換為鏈接時,我使用一些教程對此進行了編碼
<?php
$content = file_get_contents('http://phobos.apple.com/version');
$matches = "~(.*)/iP(.*)Restore.ipsw~";
if(preg_match($matches, $content, $url)) {
echo preg_replace($matches, '<a href="'.$url[0].'" rel="nofollow">'.$url[0].'</a>', $matches);
}
?>
問題在於將文本轉換為鏈接時,它僅顯示1個超鏈接,而其余部分則不會顯示。 http://codepad.org/v4YFfS7i
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.