Text between tags

how to get the text between the tags <script type=“text/javascript” and </script>.

You can use either of the following examples

<script type="text/javascript">
// Single line comment

/*
    Block comment
 */
</script>

This what u mean?


$content = '<script type="text/javascript">
// some js
$(document).ready()
{
	// More some js
	// hulabaloo..
}
</script>';

preg_match('/^<script type="text\\/javascript">(.*)<\\/script>$/s', $content, $result);

var_dump($result[1]);

You could use DomDocument also.


<?php
$html = '
<html>
  <head>
    <title>Sample</title>
  </head>
  <body>
    <h1>Sample</h1>
    <script type="text/javascript">
      $(document).ready(function(){
        alert("Foo!");
      });
    </script>
  </body>
</html>
';

$dom = new DOMDocument;
$dom->loadHTML($html);

var_dump(
  $dom->getElementsByTagName('body')->item(0)->getElementsByTagName('script')->item(0)->nodeValue
);

Is there a reason(?) why you don’t just use shorter version…


var_dump(
  $dom->getElementsByTagName('script')->item(0)->nodeValue
);

There could be a script element in the head, so it would break. :wink:

&lt;annoy&gt;Maybe he wants to get scripts in the &lt;head&gt; too?..&lt;/annoy&gt; We’ll see when Santhana Lakshmi comes back. (: