Unknown modifier ‘/’ error in PHP [duplicate]

Try using a different delimiter, say #:

preg_replace('#http://|ftp://#', 'https://', $value);

or (less recommended) escape every occurrence of the delimiter in the regex:

preg_replace('/http:\/\/|ftp:\/\//', 'https://', $value);

Also you are searching for the pattern http:///ftp:// which really does not make much sense, may be you meant http://|ftp://.

You can make your regex shorter as:

preg_replace('#(?:http|ftp)#', 'https', $value);

Understanding the error: Unknown modifier "https://stackoverflow.com/"

In your regex '/http:///ftp:///', the first / is considered as starting delimiter and the / after the : is considered as the ending delimiter. Now we know we can provide modifier to the regex to alter its default behavior. Some such modifiers are:

  • i : to make the matching case
    insensitive
  • m : multi-line searching

But what PHP sees after the closing delimiter is another / and tries to interpret it as a modifier but fails, resulting in the error.

preg_replace returns the altered string.

$value="http://foo.com";
$value = preg_replace('#http://|ftp://#', 'https://', $value);
// $value is now https://foo.com

Leave a Comment