How can I remove the (//<![CDATA[ , //]]>
) blocks; tags inside a script
element.
<script type="text/javascript">
//<![CDATA[
var l=new Array();
..........................
..........................
//]]>
</script>
Looks like it can be done with preg_replace()
but havent found a solution that works for me.
What regex would I use?
CDATA is necessary in any XML dialect, because text within an XML node is treated as a child element before being evaluated as JavaScript.
A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.
Note: CDATA is now deprecated. Do not use. The CDATA Section interface is used within XML for including extended portions of text.
The term CDATA, meaning character data, is used for distinct, but related, purposes in the markup languages SGML and XML. The term indicates that a certain portion of the document is general character data, rather than non-character data or character data with a more specific, limited structure.
You don't need regex for a static string.
Replace those parts of the texts with nothing:
$string = str_replace("//<![CDATA[","",$string);
$string = str_replace("//]]>","",$string);
The following regex will do it...
$removed = preg_replace('/^\s*\/\/<!\[CDATA\[([\s\S]*)\/\/\]\]>\s*\z/',
'$1',
$scriptText);
CodePad.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With