如何用 php 中的模式替换所有出现的字符串开头和结尾

How to replace all the occurrences of string start and end with pattern in php

我有这个字符串:

[image: alfa-romeo-giulia-quadrifoglio-2017-01.jpg]\r\n[image: downloadwww.jpg]\r\n\r\n\r\nEmail content With 2 Inline Attachments and 2 another attachments.\r\nTo : me \r\nBcc and Cc : me\r\n

我想在字符串中的任意位置替换此模式:

[image: .....]

我正在使用

preg_match_all('/\[image:\](.*?)\=(.*?)\]\]/s', $input, $matches);

此代码returns 一个空数组。请帮我替换所有出现的地方。

preg_match_all 从大字符串中提取字符串,要替换,需要使用 preg_replace.

您的字符串不包含 ]],也不包含 = 字符。

您可以使用

preg_replace('/\[image:[^]]*]/', '', $input)

PHP demo

\[image:[^]]*] 模式匹配

  • \[image: - [image: 子串
  • [^]]* - ]
  • 以外的零个或多个字符
  • ] - 一个 ] 字符。