Reputation:
Some of my customer wants to add google re-marketing code to their website and there is a panel to make it so.
I'm using strip_tags
to remove unnecessary tags from the code (security reason) but the function also removed the CDATA
.
How can I exclude it?
Sample Remarketing Code :
<script type="text/javascript">
/* <![CDATA[ */
var google_conversion_id = 123456789;
var google_conversion_label = "AAAAAAAAAAAAAAAAAAA";
var google_custom_params = window.google_tag_params;
var google_remarketing_only = true;
/* ]]> */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/123456789/?label=AAAAAAAAAAAAAAAAAAA&guid=ON&script=0"/>
</div>
</noscript>
PHP Code :
$userContent = addslashes(strip_tags($data["analytics_code"],'<script><noscript><img><div>'));
Output is like that :
<script type="text/javascript">
/* */
</script>
<script type="text/javascript" src="http://www.googleadservices.com/pagead/conversion.js">
</script>
<noscript>
<div style="display:inline;">
<img height="1" width="1" style="border-style:none;" alt="" src="//googleads.g.doubleclick.net/pagead/viewthroughconversion/123456789/?label=AAAAAAAAAAAAAAAAAAA&guid=ON&script=0"/>
</div>
</noscript>
Thanks
Upvotes: 2
Views: 714
Reputation: 1275
Unfortunately strip_tags()
is rather simple in it's functionality. Also, as a general rule, regular expressions are not able to parse HTML due to the fact that HTML, XML, etc do not fit into the class of regular languages. In other words you are left to use an HTML/XML parser to fish out the information you need.
Upvotes: 2