以下例子是删除从abc到xyz字符串
$fileContents = '123abcdefghijkl4567mnopqrstuvwxyz890';
$pattern = "/abc(.*?)xyz/";
echo preg_replace($pattern, '', $fileContents);
共14个元字符,要匹配原始字符,必须要转义
$ ---> \$
( ---> \(
) ---> \)
* ---> \*
+ ---> \+
. ---> \.
[ ---> \[
] ---> \]
? ---> \?
\ ---> \\
/ ---> \/
^ ---> \^
{ ---> \{
} ---> \}
以下是有转义元字符的例子
$fileContents = '<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><polygon class="fil0" points="';
$pattern = "/<\?xml version=\"1\.0\"(.*?)<polygon class=\"fil0\" points=\"/";
echo preg_replace($pattern, '', $fileContents);