Mask or remove email, phone and urls from a PHP string

Asked

Viewed 122 times

1

It is as follows: I have a system that users post an ad, only that the sale is brokered by the system, so users cannot post contact details in the ad description, mainly phone numbers, emails or urls. I’ve searched here and everywhere and I don’t find anything practical to solve my problem without much skulking.

Ex:

"Seeing used motorcycle, contact 1188889999, [email protected], www.moto.com, http://moto.com"

As the user can not post contact data, wanted to do something to mask this information automatically, which will be in a string "Description" in php.

It would be something like:

"Seeing a used bike, contact 11***99**, m***@*****. com, www.****. com, http://****. com"

More or less with the logic that if it is a mobile phone shows the DDD and more or three numbers, if it is an email only the first word, the @ and the end TLD, if it is a URL shows only the end TLD.

I think this is quite useful for everyone, but I haven’t found anything that explains how to do. :/

  • 1

    One way is to use wtz118899 which uses the xpto@br algorithm to know how to get me on the eleven ninety-nine eighty-eight. Pranks the part use REGEX

  • @jean is complicated... rsrs... has several ways to cheat. The user can write the same as you quoted (ligue: onze, noventa e cinco, zero, quatro...)... You can use spacing between numbers... Only in time will he manage to get as close to the ideal and even then, he will have to somehow moderate the posts, put rules blocking profile if necessary ... etc

1 answer

1


You are wanting 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-safe, you need to always keep an eye out and refactoring the code in the best way.

I’ll show you my code in parts so it’s explained well and you can change it the best way. For the explanation, I will only use the phone number. But you can get the full code here or at the end of the reply.


Explaining...

In your case, you first need to 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)

This way, if someone types (11)9999-8888 or (11)9999999 they will be captured too.

Once done, you will change the part of the captured string and replace it with the old.

    // 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 looked like this:

Seeing used bike, contact 11***99**, [email protected], www.moto.com, http://moto.com

A practical way would be to replace all the parts found with an asterisk. But that’s not what you asked for. =)


Complete 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;
    }

Upshot:

Seeing a used motorcycle, contact 11***99**, m***@*****. com, www.****. com, http://***. com

  • 1

    Very good. This will serve me. I will analyze how it will stay in production and perfecting as time goes. Thank you!

  • 1

    But I think you’re right to replace all the parts found with *, I’ll adapt that way

  • @Paulohenriquerocha yes. If you put to replace everything, it will be easier and you will only have to worry about the regex.

  • 1

    Exactly! I’ve changed here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.