Using phone numbers from a text with preg_replace

2020-03-30 08:48发布

I have no issue finding phone numbers within a text with preg_replace:

$text = preg_replace("/(\+?[\d-\(\)\s]{8,25}[0-9]?\d)/", '<strong>$1</strong>', $text);

However, I am working on a little function where the phone number will be obfuscated so I need the actual phone number, not the entire text.

The function looks like this:

$phone = '123-123-1234';

<script>
$(function(){
$('#number').click(function() {
$(this).find('span').text( $(this).data('last') );
});
});
</script>

<?php 
$trimmed_phone = substr($phone, 0, -5);
$last_digits = substr($phone, -5);

echo '<span id="number" data-last="'.$last_digits.'">'.$trimmed_phone.'<span>****<div class="showphone"> '.$lang['SHOW_NUMBER'].'</div></span></span>';
?>

It works great when I assign a phone number to it (as it's shown above). However I do not know how to implement it (how to extract phone numbers with preg_replace and then run them through the function) within a text that has both words and phone number(s).

0条回答
登录 后发表回答