views:

78

answers:

3

I need to count the no. of lines of inline java script between script tags in php files. How do I do it? Will grep linux command suffice or I can get some tool to do it? Please help.

+3  A: 

You might use a regular expression like to extract the content of each SCRIPT tag in your files and than count the \n occurrences within the content.

This regex should match all script tag including the opening and closing tag:

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

You should than remove the tags and lines without any code to count the real lines of JavaScript code.

Kau-Boy
What about `alert('</script>');` ?
hsz
Why would such a line of code make any sense? Why not giving me a good example on how my code fails like echo '</'.'script'.'>'; :) You can every find a string that would destory the matching now matter how good it is. But I think a basic idea should be enough.
Kau-Boy
How do I use it with grep can you give an example.
Geshan
I am sorry, but I am not a linux user and I don't really know how to do that with grep. But I don't think you can do it with a single grep command, as you will have to count the line breaks of the regex match.
Kau-Boy
A: 

Please take a look on the following code,it works but you may need to updates as per your requirements

<?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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;
<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>

save this php file as thisfile.php then try

Have a nice day

john
A: 

If you need to do this from a processed HTML page, use DOM to get all script tags without a src attribute. Then for each found node split the child TextNode by the linebreak or simple count them. Done.

If you need to grab this from actual PHP source code, use the Tokenizer to find T_STRINGS and similar parser tokens and analyze them for <script> blocks, but note that it might be impossible to find them all, if there is code like:

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

because that wont be analyzable as a JavaScript String before PHP processed it.

Gordon