I want to clean the comments and some other garbage or tags from the <body>
section in HTML using PHP and regex but my code not work:
$str=preg_replace_callback('/<body>(.*?)<\/body>/s',
function($matches){
return '<body>'.preg_replace(array(
'/<!--(.|\s)*?-->/',
),
array(
'',
), $matches[1]).'</body>';
}, $str);
The problem is that nothing happens. Comments will remain where they are or any cleaning to do, nothing happens. Can you help? Thanks!
EDIT:
Thanks to @mhall I figureout that my regex not work becouse of attributes in <body>
tag. I use his code and update this:
$str = preg_replace_callback('/(?=<body(.*?)>)(.*?)(?<=<\/body>)/s',
function($matches) {
return preg_replace('/<!--.*?-->/s', '', $matches[2]);
}, $str);
This work PERFECT!
Thanks people!