从 wiki 标记中删除方括号

问题描述 投票:0回答:2

我正在寻找从维基文本中删除一些标记的方法。

示例:

Mexico, officially the United Mexican States, is a [wiki=1c1ff8db21cf79377f9930e6e6ff8247]country[/wiki] in the southern portion of [wiki=5ffec2d87ab548202f8b549af380913a]North America[/wiki].

返回的文本应该是:

Mexico, officially the United Mexican States, is a country in the southern portion of North America.

我们尝试过的:

preg_replace('/(\[.*?.\])/', '', $txt)

谢谢。

php preg-replace markup
2个回答
1
投票

修改模式以替换打开标签和关闭标签之间的文本。

$markup = 'Mexico, officially the United Mexican States, is a [wiki=1c1ff8db21cf79377f9930e6e6ff8247]country[/wiki] in the southern portion of [wiki=5ffec2d87ab548202f8b549af380913a]North America[/wiki].'; $plain = preg_replace('/\[.*?\](.*?)\[\/.*?\]/', '$1', $markup); echo $plain;
输出

Mexico, officially the United Mexican States, is a country in the southern portion of North America.
    

1
投票
如果你想匹配wiki:你可以使用:

\[wiki=[^][]*](.+?)\[/wiki]

解释

  • \[wiki=
    匹配
    [wiki=
    
    
  • [^][]*
     可选择重复匹配除 
    [
    ]
     之外的任何字符
    
  • ]
     匹配右方括号
  • (.+?)
     捕获第1组,尽可能匹配1+个字符
  • \[/wiki]
    匹配
    [/wiki]
    
    
在替换使用组 1 中,如

$1


正则表达式演示

示例

$re = '`\[wiki=[^][]*](.+?)\[/wiki]`m'; $str = 'Mexico, officially the United Mexican States, is a [wiki=1c1ff8db21cf79377f9930e6e6ff8247]country[/wiki] in the southern portion of [wiki=5ffec2d87ab548202f8b549af380913a]North America[/wiki]. '; $result = preg_replace($re, '$1', $str); echo $result;
输出

Mexico, officially the United Mexican States, is a country in the southern portion of North America.
    
© www.soinside.com 2019 - 2024. All rights reserved.