php删除特定字符之间的字符

Dar*_*ney 2 php regex

我知道如何删除字符串中的特定字符但不确定以下内容:

例如字符串: Code [7845] [5589] [554] [4]

我需要删除它们之间的所有实例[以及]它们之间的所有内容,然后关闭空白区域,以便我最终简单地:Code

这可能吗?

Fai*_*Dev 5

这样做:)

$result = preg_replace('/\[.*?\]|\s*/', '', $subject);
Run Code Online (Sandbox Code Playgroud)

说明:

"
\[    # Match the character “[” literally
.     # Match any single character that is not a line break character
   *  # Between zero and unlimited times, as many times as possible, giving back as needed (greedy)
\]    # Match the character “]” literally
"
Run Code Online (Sandbox Code Playgroud)

正如@Felix所指出的,这是一个相当容易的表达.你应该看看这里,并尝试熟悉你可以使用的各种技术.

  • @ ivory-santos你需要从正则表达式中删除`|\s*`. (3认同)