正则表达式:使用preg_replace_callback忽略HTML标记

时间:2011-08-04 09:25:46

标签: php regex preg-replace-callback

我正在尝试抓取HTML标签之间的所有文本(如果有),并在其上放置一个函数.. 我的意思是..我的代码现在是

$code = preg_replace_callback('/(\(\s*\')\s*(.*?)\s*(\')/',
        function($matches) {
            return strtolower($matches);
        }, $code);

现在我想要的是:

  1. 如果有HTML标签===返回HTML标签+ strtolower(标签之间的文字)。

  2. 如果没有HTML标签===返回strtolower(所有文字)


  3. 示例: 如果我们有:

    ('TEST HERE this is a TEXT')
    

    返回

    ('test here this is a text')
    

    但是如果使用HTML标签

    <DIV CLASS='tesT'>This IS A TEXT</DIV><Div class='Test1'>THIS is another TEXT</DIV>
    

    返回

    <DIV CLASS='tesT'>this is a text</DIV><Div class='Test1'>this is another text</DIV>
    

1 个答案:

答案 0 :(得分:0)

$str = preg_replace_callback( '/(<.+?>)*(.*?)/s', 'replace', $str );

function replace( $matches ) {
    return $matches[1].strtolower( $matches[2] );
}
相关问题