Why complicated the regex:
$url = "https://example.com/products/3283284939";
if (!str($url)->startsWith('https://example.com/')) {
preg_replace(/* etc */);
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hello there.
I have a regex to hide all phone numbers, but it also hides them when someone sends a url of my website.
For example:
Test message, this is my product: https://example.com/products/3283284939
this becomes:
Test message, this is my product: https://example.com/products/hidden
Any ideas how could I instruct regex to ignore this when the match starts with this specific URL?
Hoped that this: /(?!https://www.example.com.*$)+?[0-9][0-9()-\s+]{6,12}[0-9]/
would work, but actually it doesn't.
There are thousands of different ways to write phone numbers, and most of them also coincidentally match things that aren’t phone numbers.
I don’t think there is any way to use regex to “hide phone numbers”. That’s much too broad and ill-defined a concept.
Please or to participate in this conversation.