0

I got the email and phone to be hidden individually. How can I get the two combined so that I can run a check on one string and if email and/or phone are found the replace them.

$string= 'text text [email protected] blalba blalba blalba 777 777 7777 blalba blalba';

$hide_email = preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6})/', '[hide email]', $string);
$hide_phone = preg_replace('/[(]*\d{3}[)]*\s*[.\-\s]*\d{3}[.\-\s]*\d{4}/', '[hide phone]', $string);

echo $string; //hide the email and/or phone found
3
  • 1
    Anything particularly wrong with 2 calls? (not to mention preg_replace accepts an array of regular expressions)
    – zerkms
    Sep 21, 2014 at 23:52
  • I guess nothing is wrong but how can I echo variable that contains the whole string with the hidden email/phone ?
    – Gadgetster
    Sep 21, 2014 at 23:58
  • you just apply second preg_replace not to the original string but to...
    – zerkms
    Sep 22, 2014 at 0:01

1 Answer 1

0

Copy and pasted your code above.

$string= 'text text [email protected] blalba blalba blalba 777 777 7777 blalba blalba';

$string = preg_replace('/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,6})/', '[hide email]', $string); $string = preg_replace('/[(]\d{3}[)]\s*[.-\s]\d{3}[.-\s]\d{4}/', '[hide phone]', $string);

echo $string; //hide the email and/or phone found

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.