2014-09-22 99 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