2014-09-22 100 views
5

我試圖用[隱藏]替換電話號碼,並單擊顯示它們。只有一個號碼時,它很好用。但是當更多時,它隱藏它,但問題是它爲兩個隱藏的字段返回相同的數字。PHP隱藏多個電話號碼

$check ='111 111 1111/222 222 2222';  
preg_match('/[(]*\d{3}[)]*\s*[.\- ]*\d{3}[.\- ]*\d{4}/', $check, $phone_matches); 
echo sizeOf($phone_matches); //returns 1, why not 2?? 

好看多了,如果你能幫助我把sizeOf($phone_matches)以顯示正確的金額,我應該是不錯的,從那裏!

編輯:

for($i=0; $i<sizeOf($phone_matches[0]); $i++){ 
    $check = preg_replace('/[(]*\d{3}[)]*\s*[.\- ]*\d{3}[.\- ]*\d{4}/', '<span class="hide">'.$phone_matches[0][$i].'</span><span class="show">show phone</span>', $check); 
} 

echo $check; 
+3

嘗試'preg_match_all':http://php.net/manual/en/function.preg-match-all.php – 2014-09-22 20:27:43

回答

5

你想用preg_match_all,不preg_match

preg_match_all('/[(]*\d{3}[)]*\s*[.\- ]*\d{3}[.\- ]*\d{4}/', $check, $phone_matches); 
print_r($phone_matches); 

但請注意,sizeof($phone_matches)仍然是1,因爲比賽的數組實際上是$phone_matches[0]

要通過你會做的所有比賽迭代:

foreach ($phone_matches[0] as $match) { 
    //Do something with $match 
} 

但你究竟要完成的,沒有必要爲preg_match_all可言。一個簡單的線條preg_replace會做的伎倆:

$check = preg_replace('/[(]*\d{3}[)]*\s*[.\- ]*\d{3}[.\- ]*\d{4}/', '<span class="hide">$0</span><span class="show">show phone</span>', $check); 
+0

好。現在我該如何運行$ phone_matches的支票?當我做sizeOf($ phone_matches)我得到1仍然 – BragDeal 2014-09-22 20:31:41

+0

@ user235196看我的編輯。 – AlliterativeAlice 2014-09-22 20:32:45

+0

如何檢索找到的正確數量的手機?在這種情況下,我怎麼能得到2? – BragDeal 2014-09-22 20:33:46