在PHP中删除部分字符串

时间:2013-11-23 21:52:07

标签: php regex preg-replace

如何更换内容中未使用的部分。例如:

$content = "
    Test content

    [section]
        Section Content
    [section]

    End.
";

$content = preg_replace('/\[section\](.*)\[section\]/', '', $content);

由于

2 个答案:

答案 0 :(得分:3)

s修饰符允许模式中的点元字符匹配所有字符,包括换行符。

i修饰符用于不区分大小写的匹配,允许使用大写和小写字母。通过添加量词?,它可以实现非贪婪匹配并匹配尽可能少的数量。

$content = preg_replace('/\[section\].*?\[section\]/si', '', $content);

请参阅Demo

答案 1 :(得分:2)

什么是正则表达式?这应该可以运行并且运行得更快:

$content = explode('[section]',$content);
$content = $content[0].$content[2];