You are trying to hide the contacts of "smart" users who want to make direct contact with the end customer without your service. However, this code proposal I'm going to show you is not fail-proof, you always have to keep an eye on it and refactor the code in the best way.
I'll show my code in parts so that it is well explained and you can change it in the best way. For explanation, I will only use the phone number. But you can get the full code here or at the end of the answer.
Explaining ...
In your case, you need to first find the string types (email, website, phone). For this I made these regex:
$string = "Vendo moto usada, entre em contato 1188889999, [email protected], www.moto.com, http://moto.com";
// captura todos os telefones da string
preg_match_all('/[0-9\-\(\)]+/', $string, $match_phone);
What does this REGEX say?
/[0-9\-\(\)]+/
pegue um conjunto de caracteres que pode ter apenas:
"0-9" - numeros de zero a nove
"(" - colchetes
"-" - tracinhos (não sei o nome correto)
In this way, if someone types (11) 9999-8888 or (11) 9999999 they will be caught too.
This will change the part of the captured string and replace it with the old one.
// altera os telefones encontrados
$phones = $match_phone[0];
foreach($phones as $phone){
$newPhone = alterar_telefone($phone);
$string = str_replace($phone,$newPhone,$string);
}
Function alterar_telefone()
:
function alterar_telefone($phone){
$num = 0;
$newStringPhone = "";
$positions = array(1,2,7,8); // posições que não serão substituidos
for($x = 0; $x < strlen($phone); $x++){
if(is_numeric($phone[$x])){
$num++;
if(in_array($num,$positions)){
$newStringPhone .= $phone[$x];
continue;
}
$newStringPhone .= "*";
continue;
}
$newStringPhone .= $phone[$x];
}
return $newStringPhone;
}
In this case, the string looks like this:
Used motorcycle, contact 11 **** 99 **, [email protected],
www.moto.com, link
A practical way would be to replace all parts found by an asterisk. But that's not what you asked for. =)
Full Code
$string = "Vendo moto usada, entre em contato 1188889999, [email protected], www.moto.com, http://moto.com";
// captura todos os emails da string
preg_match_all('/[A-z0-9]+@[A-z0-9\.]+/', $string, $match_email);
// captura todos os sites da string
preg_match_all('/(www\.|http:\/\/|https:\/\/)+[A-z0-9\.]+/', $string, $match_site);
// captura todos os telefones da string
preg_match_all('/[0-9\-\(\)]+/', $string, $match_phone);
// altera os emails encontrados
$emails = $match_email[0];
foreach($emails as $email){
$newEmail = alterar_email($email);
$string = str_replace($email,$newEmail,$string);
}
// altera os telefones encontrados
$phones = $match_phone[0];
foreach($phones as $phone){
$newPhone = alterar_telefone($phone);
$string = str_replace($phone,$newPhone,$string);
}
// altera os telefones encontrados
$sites = $match_site[0];
$inicioSites = $match_site[1];
for($s = 0; $s < count($sites); $s++){
$sewSite = alterar_site($sites[$s], $inicioSites[$s]);
$string = str_replace($sites[$s],$sewSite,$string);
}
echo $string;
// função que altera o email
function alterar_email($email){
$newStringEmail = "";
$partesEmail = explode("@", $email); // separar em duas partes
/*
primeira parte do email
"moto"
*/
for($y = 0; $y < strlen($partesEmail[0]); $y++){
if($y == 0) {
$newStringEmail .= $partesEmail[0][0];
} else {
$newStringEmail .= "*";
}
}
$newStringEmail .= "@"; // adicionando o "@" que foi perdido
/*
segunda parte do email
"usada.com"
*/
$parts2 = explode(".", $partesEmail[1]);
for($z = 0; $z < strlen($parts2[0]); $z++){
$newStringEmail .= "*";
}
for($x = 1; $x < count($parts2); $x++){
$newStringEmail .= ".".$parts2[$x];
}
return $newStringEmail;
}
function alterar_site($site, $inicio){
$newStringSite = $inicio;
$parteInicialSite = explode($inicio,$site);
$partesSite = explode(".",$parteInicialSite[1]);
for($x = 0; $x < strlen($partesSite[0]); $x++){
$newStringSite .= "*";
}
for($y = 1; $y < count($partesSite); $y++){
$newStringSite .= ".".$partesSite[$y];
}
return $newStringSite;
}
function alterar_telefone($phone){
$num = 0;
$newStringPhone = "";
$positions = array(1,2,7,8); // posições que não serão substituidos
for($x = 0; $x < strlen($phone); $x++){
if(is_numeric($phone[$x])){
$num++;
if(in_array($num,$positions)){
$newStringPhone .= $phone[$x];
continue;
}
$newStringPhone .= "*";
continue;
}
$newStringPhone .= $phone[$x];
}
return $newStringPhone;
}
Result:
If you are selling a used motorcycle, please contact 11 **** 99 **, m***@*****.com,
www. ****. com, http: //****.com