简体   繁体   English

从 wiki 标记中删除方括号

[英]Remove square brackets from wiki markup

I'm looking for the way to remove some markup from wikitext.我正在寻找从 wikitext 中删除一些标记的方法。

Example:例子:

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

Returned text should be:返回的文本应该是:

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

What we tried:我们尝试了什么:

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

Thanks.谢谢。

Modify the pattern to replace the text between the open and close tags.修改模式以替换打开和关闭标记之间的文本。

$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;

Output输出

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

If you want to match the wiki: you can use:如果你想匹配wiki:你可以使用:

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

Explanation解释

  • \[wiki= Match [wiki= \[wiki=匹配[wiki=
  • [^][]* Optionally repeat matching any char except [ or ] [^][]*可选择重复匹配除[]之外的任何字符
  • ] Match the closing square bracket ]匹配右方括号
  • (.+?) Capture group 1, match 1+ chars as least as possible (.+?)捕获组 1,尽可能少匹配 1+ 个字符
  • \[/wiki] Match [/wiki] \[/wiki]匹配[/wiki]

In the replacement use group 1 like $1在替换使用组 1 中,例如$1

Regex demo正则表达式演示

Example例子

$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;

Output输出

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM