<?php
$string = "Vendo moto usada, entre em contato 1188889999, moto@usada.com, www.moto.com, http://m...content-available-to-author-only...o.com";
// captura todos os emails da string
// captura todos os sites da string
preg_match_all('/(www\.|http:\/\/|https:\/\/)+[A-z0-9\.]+/', $string, $match_site);
// captura todos os telefones da string
// altera os emails encontrados
$emails = $match_email[0];
foreach($emails as $email){
$newEmail = alterar_email($email);
}
// altera os telefones encontrados
$phones = $match_phone[0];
foreach($phones as $phone){
$newPhone = alterar_telefone($phone);
}
// 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]);
}
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++){ $num++;
$newStringPhone .= $phone[$x];
continue;
}
$newStringPhone .= "*";
continue;
}
$newStringPhone .= $phone[$x];
}
return $newStringPhone;
}
PD9waHAgCgoJCSRzdHJpbmcgPSAiVmV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