简体   繁体   中英

PHP Removing Phone Number and E-mail from a string

I m looking for a way to remove any phone number and E-mail adress from a string.

Is anybody who have already realize that kind of function ?

What do you all recommand ? EREG ?

Thanks Regards, Titus.

PS: any tuto is huge welcomed :)

    $chaine = "Here is my e-Mail contact@domain.eu and my phone (works) 0606060606 (not works) 06.06.06.06.06";
// Phone Number :
$chaine = preg_replace('/\+?[0-9][0-9()-\s+]{4,20}[0-9]/', '[removed]', $chaine);

// E-mail adress :
$pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
$replacement = "[removed]";
preg_replace($pattern, $replacement, $chaine);

echo $chaine;

Here's a basic implementation. The regex for email is from regexlib . A phpfiddle can be tested here .

function removeEmailAndPhoneFromString($string) {
    // remove email
    $string = preg_replace('/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/','',$string);

    // remove phone
    $string = preg_replace('/([0-9]+[\- ]?[0-9]+)/','',$string);

    return $string;
}

echo removeEmailAndPhoneFromString('Lorem ipsum foo@bar.com dolor sit amet, 555-123 555 consectetur adipiscing elit.');

The regex for the phone was just thrown together so YMMV.

Try to use preg_replace for this.

The code would like a

    $string = 'your string';

    $patterns = array();
    $patterns[0] = '/([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)/';
    $patterns[1] = '/([0-9]+[\- ]?[0-9]+)/';

    $replacements = array();
    $replacements[0] = '';
    $replacements[1] = '';

    //should use just one call of preg_replace for perfomance issues
    $string = preg_replace($patterns, $replacements, $string);

Also read

This questions about regexp for validate phone numbers and email adress.

psSample on phpfiddle

You can use this booth functions

function hide_email($str){

   $pattern = "/[^@\s]*@[^@\s]*\.[^@\s]*/";
   $replacement = "[Email removed]";
   return = preg_replace($pattern, $replacement, $str);

 }

in the hide_telnr function there ist {4,} to tell that numbers lower 99999 are allowed

function hide_telnr($str){

   $pattern = "/([0-9]+[\- ]?[0-9]+){4,}/";
   $replacement = "[Telefon Number removed]";
   return = preg_replace($pattern, $replacement, $str);

 }

If you want to hide email and phone numbers from your Messages or Chat in PHP or any other language. You need to use regular expressions, read about regex on w3school .

I have an easy and complete solution for you.

<?php       
    $regex_email = '/[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})/';
    $regex_phone = "/[0-9]{5,}|\d[ 0-9 ]{1,}\d|\sone|\stwo|\sthree|\sfour|\sfive|\ssix|\sseven|\seight|\snine|\sten/i";

    
    $str = " Hello My Email soroutlove1996@gmail.com AND Phone No. is +919992799999 and +91 9992799999, or 9 9 9 2 7 9 9 9 9 9 and Nine Nine";;
    $str = preg_replace($regex_email,'(email hidden)',$str); // extract email
    $str = preg_replace($regex_phone,'(phone hidden)',$str); // extract phone
    
    echo $str;

Output: Hello My Email (email hidden) AND Phone No. is +(phone hidden) and +(phone hidden), or (phone hidden) and(phone hidden)(phone hidden)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM