2014-04-17 78 views
0

我有一個文本文件,我提取了所有域地址與http:// 現在我想要替換所有http://。在我的比賽陣列「」但是什麼也沒有發生IM甚至沒有得到一個錯誤從未運行「preg_replace()」無法正常工作。

$list = file_get_contents('file.txt'); 
preg_match_all("/http:\/\/.([a-z]{1,24}).([a-z^0-9-]{1,23}).([a-z]{1,3})/", $list, $matches); 

for ($i=0; $i>=50; $i++) { 
    $pattern = array(); 
    $replacement = array(); 
    $pattern[0][$i] = "/http:\/\/.[w-w]{1,3}/"; 
    $replacement[0][$i] = ''; 

    preg_replace($pattern[0][$i], $replacement[0][$i], $matches[0][$i]); 
} 

print_r($matches); 
+2

你能告訴我們你的輸入「file.txt」嗎? – Helpful

+1

你確定你的循環條件正確嗎?對於($ i = 0; $ i> = 50; $ i ++){'是一個非操作對象,這個循環從不執行 – Ibrahim

+4

'。 – Toto

回答

1

你的循環,因爲0 >= 50產生false。這就是說,你要尋找的是一個地圖操作:

$matches = array_map(function($match) { 
    return preg_replace('~^http://w{1,3}~', '', $match); 
}, $matches[0]); 
print_r($matches); 
+0

非常感謝您的好意,先生;)做到了。循環我總是嘗試這兩個<=|> =因爲我忘了什麼吃什麼每次 – user3434985

+0

@ user3434985:您的匹配域名的正則表達式也是不正確的。請參閱[我的評論](http://stackoverflow.com/questions/23126647/why-isnt-preg-replace-replacing-the-string#comment35357186_23126647)下的問題。 –

0

preg_match_all也有問題。正則表達式中的時間段與任何字符匹配。

$list = file_get_contents('file.txt'); 
preg_match_all("/http:\/\/([a-z]{1,24})\.([a-z^0-9-]{1,23})\.([a-z]{1,3})/", $list, $matches); 

$pattern = "/http:\/\/(.[w-w]{1,3})/"; 
$replacement = '$1'; 
$matches[0] = preg_replace($pattern, $replacement, $matches[0]); 

print_r($matches);