bomanden
bomanden

Reputation: 314

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?

Upvotes: 14

Views: 48202

Answers (8)

pulzarraider
pulzarraider

Reputation: 2317

If <![CDATA[ contains some html special character, e.g. &, ", ', <, > and you will work with the rest of the string as it is still XML, you should escape those chars. Otherwise you will make your XML invalid.

function removeCDataFromString(string $string)
{
    return preg_replace_callback(
        '~<!\[CDATA\[(.*)\]\]>~',
        function (array $matches) {
            return htmlspecialchars($matches[1], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
        },
        $string
    );
}

Upvotes: 3

duttyman
duttyman

Reputation: 151

$nodeText = '<![CDATA[some text]]>';
$text = removeCdataFormat($nodeText);    

public function removeCdataFormat($nodeText)
{
    $regex_replace = array('','');
    $regex_patterns = array(
        '/<!\[CDATA\[/',
        '/\]\]>/'
   );
   return trim(preg_replace($regex_patterns, $regex_replace, $nodeText));
}

Upvotes: 0

Fthr
Fthr

Reputation: 807

I use like this to remove <![CDATA[]] but on single line now work for me, dont know if for multiple line string.

preg_match_all('/CDATA\[(.*?)\]/', $your_string_before_this, $datas); 
$string_result_after_this = $datas[1][0];

Upvotes: 0

Siddharth
Siddharth

Reputation: 869

use str_replace() instead of preg_replace() it's lot easier

$var = str_replace('<![CDATA[', '', $var);
$var = str_replace(']]','',$var);
echo $var;

Upvotes: 1

Rohan Kumar
Rohan Kumar

Reputation: 40639

You can also try,

$s=str_replace(array("//<![CDATA[","//]]>"),"",$s);

Upvotes: 2

dimme
dimme

Reputation: 4424

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);

Upvotes: 21

Alan Moore
Alan Moore

Reputation: 75252

If you must...

$s = preg_replace('~//<!\[CDATA\[\s*|\s*//\]\]>~', '', $s);

This will remove the whole line containing each tag without messing up the indentation of the enclosed code.

Upvotes: 6

alex
alex

Reputation: 490471

The following regex will do it...

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

CodePad.

Upvotes: 13

Related Questions