preg replace - PHP hiding multiple phone numbers -
i trying replace phone numbers [hidden] , show them on click. works great when there 1 number. when there more, hides it, problem returns same number both hidden fields.
$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??
pretty much, if can me sizeof($phone_matches)
show correct amount, should there!
edit:
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;
you want use preg_match_all
, not preg_match
preg_match_all('/[(]*\d{3}[)]*\s*[.\- ]*\d{3}[.\- ]*\d{4}/', $check, $phone_matches); print_r($phone_matches);
but note sizeof($phone_matches)
still 1, since array of matches $phone_matches[0]
.
to iterate through matches do:
foreach ($phone_matches[0] $match) { //do $match }
but you're actually trying accomplish, there's no need preg_match_all
@ all. simple one-line preg_replace
trick:
$check = preg_replace('/[(]*\d{3}[)]*\s*[.\- ]*\d{3}[.\- ]*\d{4}/', '<span class="hide">$0</span><span class="show">show phone</span>', $check);
Comments
Post a Comment