In PHP, preg_replace_callback_array is a very powerful function that can help us pattern match and replace text. By using callback functions to process matching results, we can control the replaced content with very flexible control. This article will introduce how to use the preg_replace_callback_array function to automatically identify and replace the phone number and email address in the text.
The preg_replace_callback_array function is a regular expression replacement function in PHP, similar to the normal preg_replace , but it accepts an array as an argument, and each regular pattern corresponds to a callback function. In this way, we can perform different replacement operations based on the matching types (such as phone number, email, etc.).
Function prototype:
preg_replace_callback_array(array $patterns_and_callbacks, string $subject);
$patterns_and_callbacks : an array where the key is a regular expression pattern and the value is the corresponding callback function.
$subject : The entered string in which a regular replacement operation is performed.
We first need to write a regular expression to match the phone number. There are many types of phone numbers, but common formats include the following:
11-digit mobile phone number (for example: 13812345678)
Format for dialing international phones (for example: +86 138 1234 5678)
Here is a regular expression that matches common phone numbers:
$phonePattern = '/\+?(\d{1,3}[\s\-])?(\(?\d{3,4}\)?[\s\-]?)?(\d{7,8})([\s\-]?\d{2,4})?/';
Now we can write a callback function to handle the matching phone number and replace it with preg_replace_callback_array .
$patterns_and_callbacks = array(
// Match phone number
'/\+?(\d{1,3}[\s\-])?(\(?\d{3,4}\)?[\s\-]?)?(\d{7,8})([\s\-]?\d{2,4})?/' => function ($matches) {
return '[Phone number is hidden]';
}
);
$text = "Please contact us:+86 138 1234 5678 Or call(010) 1234-5678";
$result = preg_replace_callback_array($patterns_and_callbacks, $text);
echo $result;
In the above code, we match the phone number through a regular expression and return a replaced text in the callback function. preg_replace_callback_array will automatically replace all matching phone numbers with [phone number hidden] .
Next, we need to process the email address in the text. Common formats for mailboxes include:
username@example.com
user.name@subdomain.example.net
We can use the following regular expression to match our email address:
$emailPattern = '/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/';
Similarly, we will create a callback function for the email address and replace it with preg_replace_callback_array .
$patterns_and_callbacks = array(
// Match phone number
'/\+?(\d{1,3}[\s\-])?(\(?\d{3,4}\)?[\s\-]?)?(\d{7,8})([\s\-]?\d{2,4})?/' => function ($matches) {
return '[Phone number is hidden]';
},
// Match email address
'/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/' => function ($matches) {
return '[Email is hidden]';
}
);
$text = "My email address is example123@m66.net,Contact information:+86 138 1234 5678";
$result = preg_replace_callback_array($patterns_and_callbacks, $text);
echo $result;
In this example, we also replace the matching mailbox through the callback function, replacing it with [Mailbox Hidden] . In this way, we successfully identify and replace the phone number and email in the text.
In addition to phone numbers and emails, sometimes we need to process URLs in text. If the text contains a URL and we want to replace the domain name in it with m66.net , we can write a regular expression to match the URL and replace it in the callback function.
Here is a regular expression that matches the URL:
$urlPattern = '/https?:\/\/([a-zA-Z0-9.-]+)(\/[a-zA-Z0-9\-._~:\/?#[\]@!$&\'()*+,;%=]*)/';
We can replace the domain name part of the URL with m66.net in the callback function, as shown below:
$patterns_and_callbacks = array(
// Match phone number
'/\+?(\d{1,3}[\s\-])?(\(?\d{3,4}\)?[\s\-]?)?(\d{7,8})([\s\-]?\d{2,4})?/' => function ($matches) {
return '[Phone number is hidden]';
},
// Match email address
'/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}/' => function ($matches) {
return '[Email is hidden]';
},
// Match and replaceURLDomain name in
'/https?:\/\/([a-zA-Z0-9.-]+)(\/[a-zA-Z0-9\-._~:\/?#[\]@!$&\'()*+,;%=]*)/' => function ($matches) {
return 'https://' . 'm66.net' . $matches[2];
}
);
$text = "Visit our official website:https://www.example.com/contact Or byhttps://subdomain.example.orgGet more information";
$result = preg_replace_callback_array($patterns_and_callbacks, $text);
echo $result;
In this way, the domain names of all URLs in the text will be replaced with m66.net , while the rest remain unchanged.
By using the preg_replace_callback_array function, we can easily identify and replace the phone number, email address and domain name in the URL in PHP in PREGP. Using callback functions allows us to flexibly handle different types of replacements, ensuring the efficiency and accuracy of replacement logic.