首页 > 解决方案 > Replace characters in a PHP string skipping specific domain extensions

问题描述

I have a long string and would like to replace every dot "." with a question mark, however my string includes domain extensions such as .com which I would like to skip when replacing.

Is there any way I can provide an array such as (".com", ".net", ".org") of phrases to skip when replacing using str_replace() or a similar function?

Input sentence:

$string = "An example of a website would be google.com or similar. However this is not what we are looking for";

The following:

str_replace(".", "?", $string);

Produces:

An example of a website would be google?com or similar? However this is not what we are looking for

Desired output:

An example of a website would be google.com or similar? However this is not what we are looking for

I would like to provide an array of domain extensions to skip, when replacing. Such as:

$skip = array(".com",".net",".org");

and wherever those appear, don't substitute the dot with a question mark.

EDIT: Looks like I need to use a negative lookahead with preg_replace. However not sure how to put it all together: "look for a full stop that is NOT followed by COM or NET or ORG.

标签: phpregexreplacepreg-replacestr-replace

解决方案


You need

$result = preg_replace('~\.(?!(?:com|org|net)\b)~', '?', $string);

See the regex demo. Details

  • \. - a dot
  • (?! - not followed with
    • (?:com|org|net) - com, org, net substrings...
    • \b - as whole words (it is a word boundary)
  • ) - end of the negative lookahead.

NOTE: to make the TLDs match in a case insensitive way, add i after the trailing regex delimiter, here, ~i.

See a PHP demo:

$string = "An example of a website would be google.com or similar. However this is not what we are looking for";
$tlds = ['com', 'org', 'net'];
echo preg_replace('~\.(?!(?:' . implode('|', $tlds) . ')\b)~i', '?', $string);
// => An example of a website would be google.com or similar? However this is not what we are looking for

推荐阅读