在 PHP使用正则表达式替换从 abc 到 xyz 的字符串

以下例子是删除从abc到xyz字符串
	$fileContents = '123abcdefghijkl4567mnopqrstuvwxyz890';
	$pattern = "/abc(.*?)xyz/";  // 匹配从 acb 到 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=\"/";  // 匹配从 <?xml version="1.0" 到 <polygon class="fil0" points="
echo preg_replace($pattern, '', $fileContents);

相关推荐

  1. PHP使用表达式替换 abc xyz 字符串

    2024-05-09 09:42:02       32 阅读
  2. CPy:Python字符串表达式

    2024-05-09 09:42:02       34 阅读
  3. 替换表达式c#

    2024-05-09 09:42:02       31 阅读
  4. PHP常用表达式

    2024-05-09 09:42:02       67 阅读
  5. 表达式前端中使用

    2024-05-09 09:42:02       62 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 09:42:02       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 09:42:02       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 09:42:02       87 阅读
  4. Python语言-面向对象

    2024-05-09 09:42:02       96 阅读

热门阅读

  1. Oracle Patch清理

    2024-05-09 09:42:02       25 阅读
  2. Gateway的基本配置

    2024-05-09 09:42:02       36 阅读
  3. 电子邮件协议

    2024-05-09 09:42:02       31 阅读
  4. Centos7 将home空间分配给根目录(/dev/centos/root)

    2024-05-09 09:42:02       29 阅读
  5. conda搭建torch开发环境

    2024-05-09 09:42:02       34 阅读
  6. 【C++】GNU Debugger (GDB) 使用示例

    2024-05-09 09:42:02       36 阅读
  7. Shader 纹理动画和顶点动画

    2024-05-09 09:42:02       28 阅读
  8. el-table-column表格匹配字典数据

    2024-05-09 09:42:02       24 阅读
  9. vue3+element-plus国际化

    2024-05-09 09:42:02       32 阅读