Search code examples
phpregexvalidationpreg-replaceurl-validation

Hide any URL contained in a given string except one


I have a booking site with a small chat system that allows potential guests to message hosts, for example to ask whether a room is available.

I want that any URL typed by users is hidden (= replaced by ****) except if it's a URL of my site domain. I currently have a regular expression that successfully match any URL:

    $replacing_string = '****';

    $userMessage = preg_replace('{\b(?:http://)?(www\.)?([^\s]+)(\.com|\.org|\.net|\.it)\b}mi', $replacing_string, $userMessage);

I need to edit it so I can match all URLs except those containing mydomain.com

Expected results for these strings:

"Hi, check my site instead www.othersite.com/room/123"

after preg_replace:

"Hi, check my site instead ****"


"Hi, is this room available? www.mydomain.com/room/123"

after preg_replace:

"Hi, is this room available? www.mydomain.com/room/123"


Solution

  • Negative Lookahead

    We can do this with minimal changes to your existing regex:

    $userMessage = preg_replace('(?!.*mydomain\.com){\b(?:http://)?(www\.)?([^\s]+)(\.com|\.org|\.net|\.it)\b}mi', $replacing_string, $userMessage);
    

    The negative lookahead (?!.*mydomain\.com) asserts that what follows is not some chars then mydomain.com

    Reference