Hello,
I have a project that uses file_get_contents();
on an external page and would like to know how I can remove, for example, all lines that start with //
.
The external page contains comments in JavaScript's and I have not found anything that can remove only comments within the tag.
Sample external page code;
<!DOCTYPE html>
<html>
<body>
<script>
[...]
// Alerta
alert('Olá!');
[...]
</script>
</body>
</html>
Code that I tried to use to remove comments;
$html = explode(PHP_EOL, file_get_contents('https://example.com/'));
foreach($html as $linha) {
if(substr($linha, 0, 2) !== '//' && mb_substr($linha, 0, 2) !== '//') {
$linhas[] = trim($linha);
}
}
$html = implode(PHP_EOL, $linhas);
echo $html;
When I ran the script through the browser to see if it removed the comment or not, unfortunately I still got the comment.
I'm using XAMPP in the 5.6.35
version.