开发者

Regex : ignore HTML Tags with preg_replace_callback

开发者 https://www.devze.com 2023-03-25 15:50 出处:网络
I\'m trying to grab all text between the HTML Tags (if there), and put a function on it .. i mean.. my code now is

I'm trying to grab all text between the HTML Tags (if there), and put a function on it .. i mean.. my code now is

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

now what i want is :

  1. If there is a HTML tags === Return HTML tags + strtolower(for the text between the tags).

  2. If there isn't a HTML tags === Return strtolower(all the t开发者_运维技巧ext)


example : if we have :

('TEST HERE this is a TEXT')

return

('test here this is a text')

but if with HTML tags like

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

return

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


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

function replace( $matches ) {
    return $matches[1].strtolower( $matches[2] );
}
0

精彩评论

暂无评论...
验证码 换一张
取 消