preg_replace两个标签之间的几个字符

时间:2015-08-31 06:12:20

标签: php preg-replace

this is text before the tag \r \t
    \begin{aligned}\t \r \r
    \left(\frac{130}{100}x\right)  \t
    \end{aligned}
this is text after the tag \r \t 

我想删除\r\t之间\n\begin\end的所有出现。我怎样才能使用preg_replace

2 个答案:

答案 0 :(得分:1)

根据OP的澄清,\r\n\t是文字,不应与相应的特殊字符混淆

<?php
$str = <<<'EOT'
this is text before the tag \r \t
    \begin{aligned}\t \r \r
    \left(\frac{130}{100}x\right)  \t
    \end{aligned}
this is text after the tag \r \t
\begin another \r\n\t\end
EOT;

echo
    '<pre>' .
    preg_replace_callback(
        '#\\\\begin.*?\\\\end#s', // The regular expression
        function ($matches) {
            return str_replace(array('\t', '\r', '\n'), '', $matches[0]); // removes all the specified literals
        },
        $str
    ) .
    '</pre>';
?>

preg_replace_callback()中的正则表达式匹配\begin\end字符串(包括)之间的所有文本。在模式中使用s modifier会导致dot metacharacter不排除新的行字符。

对于找到的每个匹配项,该函数调用第二个参数中指定的匿名函数,该函数使用对str_replace()函数的调用来删除文字。

输出:

this is text before the tag \r \t
    \begin{aligned}  
    \left(\frac{130}{100}xight)  
    \end{aligned}
this is text after the tag \r \t
\begin another \end

答案 1 :(得分:0)

这个想法是使用\G锚来获得连续的结果。达到\end时,连续性就会被破坏。第一场比赛从分支(1)开始。

$str = <<<'EOD'
this is text before the tag \r \t
    \begin{aligned}\t \r \r
    \left(\frac{130}{100}x\right)  \t
    \end{aligned}
this is text after the tag \r \t 
EOD;

$pattern = <<<'EOD'
~
(?:
    \G(?!\A) # other occurrences are contiguous (2)
  |
    \\begin\b # the first occurrence of \t \r or \n follows "\begin" (1)
)
[^\\]* # all that is not a slash
(?:
    \\ (?!(?:[trn]|end)\b) # a slash not followed by "t","r","n" or "end"
    [^\\]*
)* (*SKIP)
\K # remove all characters on the left from the whole match
\\ [trn]
~xS
EOD;

$result = preg_replace($pattern, '', $str);

如果您使用\\begin\b

更改[^\\]*(?:\\(?!begin\b)[^\\]*)*+\\begin\b,则可以改善模式