Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove `//<![CDATA[` and end `//]]>`?

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?

like image 722
bomanden Avatar asked Nov 27 '11 04:11

bomanden


People also ask

Is CDATA necessary?

CDATA is necessary in any XML dialect, because text within an XML node is treated as a child element before being evaluated as JavaScript.

What does <![ CDATA in XML mean?

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.

Is CDATA deprecated?

Note: CDATA is now deprecated. Do not use. The CDATA Section interface is used within XML for including extended portions of text.

What is the meaning of CDATA?

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.


2 Answers

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);
like image 164
Dimme Avatar answered Oct 01 '22 11:10

Dimme


The following regex will do it...

$removed = preg_replace('/^\s*\/\/<!\[CDATA\[([\s\S]*)\/\/\]\]>\s*\z/', 
                        '$1', 
                        $scriptText);

CodePad.

like image 43
alex Avatar answered Oct 01 '22 12:10

alex