我正在寻找从维基文本中删除一些标记的方法。
示例:
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)
谢谢。
修改模式以替换打开标签和关闭标签之间的文本。
$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.
\[wiki=[^][]*](.+?)\[/wiki]
解释
\[wiki=
匹配
[wiki=
[^][]*
可选择重复匹配除
[
或
]
之外的任何字符
]
匹配右方括号
(.+?)
捕获第1组,尽可能匹配1+个字符
\[/wiki]
匹配
[/wiki]
$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.