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

8

14

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?

Groundspeed answered 27/11, 2011 at 4:9 Comment(3)
Just curious why you want to remove those two lines?Parallelize
bomanden: @JonathanM is right, you may not need to remove these elements. See When is a CDATA section necessary within a script tag? and Is CDATA really necessary?. Think it over.Typography
Ok - Its just that the Javascript dont fire .. so the code is not executed .. It is when I use Alan's solution. But thanks on the info.Groundspeed
C
13

The following regex will do it...

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

CodePad.

Countess answered 27/11, 2011 at 4:12 Comment(1)
Hi Alex .. No unfortunately not. Do know why - but got a Alans working. Perhaps you can see the difference between the two solutions. Thank you for your input.Groundspeed
J
21

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);
Jessikajessup answered 27/11, 2011 at 4:51 Comment(2)
What if it includes that text as part of the script body, perhaps as a string?Countess
I like this solution far better than the Regex option, much cleaner to read.Amphisbaena
C
13

The following regex will do it...

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

CodePad.

Countess answered 27/11, 2011 at 4:12 Comment(1)
Hi Alex .. No unfortunately not. Do know why - but got a Alans working. Perhaps you can see the difference between the two solutions. Thank you for your input.Groundspeed
I
6

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.

Ipa answered 27/11, 2011 at 4:42 Comment(0)
D
3

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
    );
}
Dodiedodo answered 2/1, 2020 at 16:15 Comment(0)
E
2

You can also try,

$s=str_replace(array("//<![CDATA[","//]]>"),"",$s);
Eluvium answered 8/11, 2013 at 12:42 Comment(0)
G
1

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

$var = str_replace('<![CDATA[', '', $var);
$var = str_replace(']]','',$var);
echo $var;
Gavotte answered 4/7, 2013 at 13:37 Comment(0)
X
0

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];
Xanthochroid answered 30/12, 2016 at 14:35 Comment(0)
N
0
$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));
}
Nereidanereids answered 12/7, 2017 at 16:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.