How to count javascript line count in php files?

I need to recount the number. lines of inline java script between script tags in php files. How should I do it? Will the grep linux command suffice or can I get some tool for this? Please, help.

+5
source share
3 answers

You can use a regular expression, for example, to extract the contents of each SCRIPT tag in your files and count the occurrence of \ n in the contents.

This regular expression must match all SCRIPT tags, including the open and close tags:

 /<script[^>]*?>(.*)?</script>/sm

You should remove tags and lines without code to count the actual lines of JavaScript code.

+3

, , , .

<?php
$file = file('thisfile.php');
for($i=0;$i<count($file);$i++)
{
    if(trim($file[$i]) == "<script language=\"javascript\">")
    {
        $start = $i.'<br>';
    }
    if(trim($file[$i]) == "</script>")
    {
        $end = $i.'<br>';
    }

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script language="javascript">
var a = 10;
var b = 10;
var c = 10;
var d = 10;
var e = 10;
var e = 10;
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<?php echo ($end - $start)-1; ?>
</body>
</html>

php thisfile.php,

0

HTML-, DOM, script src. node TextNode . .

PHP, Tokenizer, T_STRINGS <script>, , , :

 echo '<' . $scriptTag . '>' . $code . '</' . $scriptTag . '>';

JavaScript , PHP .

0

All Articles