<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Escaping JavaScript for PHP</title>
	<atom:link href="http://sixohthree.com/241/escaping/feed" rel="self" type="application/rss+xml" />
	<link>http://sixohthree.com/241/escaping</link>
	<description>The Weblog of Adam Backstrom</description>
	<lastBuildDate>Thu, 04 Mar 2010 19:43:03 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: JN</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-3343</link>
		<dc:creator>JN</dc:creator>
		<pubDate>Fri, 13 Nov 2009 23:35:22 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-3343</guid>
		<description>&lt;code&gt;
function js_escape($str) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;for($i = 0, $l = strlen($str), $new_str=&#039;&#039;; $i &lt; $l; $i++)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$new_str .= (ord(substr($str, $i, 1)) &lt; 16 ? &#039;\\x0&#039; : &#039;\\x&#039;)
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;. dechex(ord(substr($str, $i, 1)));
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;return $new_str;
}
&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p><code><br />
function js_escape($str) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0, $l = strlen($str), $new_str=''; $i &lt; $l; $i++)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= (ord(substr($str, $i, 1)) &lt; 16 ? &#039;\\x0&#039; : &#039;\\x&#039;)<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;. dechex(ord(substr($str, $i, 1)));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return $new_str;<br />
}<br />
</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nightfly</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-292</link>
		<dc:creator>Nightfly</dc:creator>
		<pubDate>Fri, 22 May 2009 22:06:54 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-292</guid>
		<description>For super tidy string iteration, I always use this:

for($i = 0, $l = strlen($str); $i &lt; $l; $i++) {
&#160;&#160;&#160;&#160;$new_str .= ‘\\x’ . dechex(ord(substr($str, $i, 1)));
}</description>
		<content:encoded><![CDATA[<p>For super tidy string iteration, I always use this:</p>
<p>for($i = 0, $l = strlen($str); $i &lt; $l; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= ‘\\x’ . dechex(ord(substr($str, $i, 1)));<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Backstrom</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-291</link>
		<dc:creator>Adam Backstrom</dc:creator>
		<pubDate>Thu, 31 Jul 2008 20:56:45 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-291</guid>
		<description>tedivm: good point. I&#039;ve updated the post to reflect your suggestion.</description>
		<content:encoded><![CDATA[<p>tedivm: good point. I&#8217;ve updated the post to reflect your suggestion.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: tedivm</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-289</link>
		<dc:creator>tedivm</dc:creator>
		<pubDate>Thu, 31 Jul 2008 18:17:39 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-289</guid>
		<description>You should take the &quot;strlen($str)&quot; check and put it outside of the for loop. The reason is that function calls use up a lot more resources than simple variable checks, and where the strlen function currently is it will be called for each interation of the loop (so if the string has a length of 50, that function will be called fifty times).

$strLen = strlen($str);
for($i = 0; $i &lt; $strLen; $i++) {
&#160;&#160;&#160;&#160;$new_str .= &#039;\\x&#039; . dechex(ord(substr($str, $i, 1)));
}</description>
		<content:encoded><![CDATA[<p>You should take the &#8220;strlen($str)&#8221; check and put it outside of the for loop. The reason is that function calls use up a lot more resources than simple variable checks, and where the strlen function currently is it will be called for each interation of the loop (so if the string has a length of 50, that function will be called fifty times).</p>
<p>$strLen = strlen($str);<br />
for($i = 0; $i &lt; $strLen; $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= &#8216;\\x&#8217; . dechex(ord(substr($str, $i, 1)));<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pascal</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-290</link>
		<dc:creator>Pascal</dc:creator>
		<pubDate>Thu, 31 Jul 2008 13:23:12 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-290</guid>
		<description>I see. And I guess using htmlentities(strip_tags($myVar)) would break the javascript. Good script then.</description>
		<content:encoded><![CDATA[<p>I see. And I guess using htmlentities(strip_tags($myVar)) would break the javascript. Good script then.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Adam Backstrom</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-288</link>
		<dc:creator>Adam Backstrom</dc:creator>
		<pubDate>Thu, 31 Jul 2008 12:41:44 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-288</guid>
		<description>Pascal,

Here&#039;s an example of &lt;a href=&quot;http://static.bwerp.net/~adam/2008/07/31/js.php&quot; rel=&quot;nofollow&quot;&gt;addslashes() allowing XSS&lt;/a&gt;. It&#039;s not sufficient to block all hack attempts. That page will load off-domain JavaScript in Firefox 3.0.1, Opera 9.51, Safari 3.1.2, and Internet Explorer 7.0.6001.18000.</description>
		<content:encoded><![CDATA[<p>Pascal,</p>
<p>Here&#8217;s an example of <a href="http://static.bwerp.net/~adam/2008/07/31/js.php" rel="nofollow">addslashes() allowing XSS</a>. It&#8217;s not sufficient to block all hack attempts. That page will load off-domain JavaScript in Firefox 3.0.1, Opera 9.51, Safari 3.1.2, and Internet Explorer 7.0.6001.18000.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pascal</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-287</link>
		<dc:creator>Pascal</dc:creator>
		<pubDate>Thu, 31 Jul 2008 01:38:45 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-287</guid>
		<description>Why not just use addslashes($myvar)?</description>
		<content:encoded><![CDATA[<p>Why not just use addslashes($myvar)?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jake Cebula</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-284</link>
		<dc:creator>Jake Cebula</dc:creator>
		<pubDate>Fri, 09 May 2008 01:33:57 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-284</guid>
		<description>Guess who stumbled upon this useful post :) Not the first time you&#039;ve helped me out, old friend. Thanks!</description>
		<content:encoded><![CDATA[<p>Guess who stumbled upon this useful post <img src='http://sixohthree.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Not the first time you&#8217;ve helped me out, old friend. Thanks!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Blech</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-285</link>
		<dc:creator>Blech</dc:creator>
		<pubDate>Fri, 25 Apr 2008 16:52:07 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-285</guid>
		<description>This one handles newlines and tabs. ^ There should be a preview button here!</description>
		<content:encoded><![CDATA[<p>This one handles newlines and tabs. ^ There should be a preview button here!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Blech</title>
		<link>http://sixohthree.com/241/escaping/comment-page-1#comment-286</link>
		<dc:creator>Blech</dc:creator>
		<pubDate>Fri, 25 Apr 2008 16:50:25 +0000</pubDate>
		<guid isPermaLink="false">/?p=241#comment-286</guid>
		<description>&lt;blockquote cite=&quot;This one handles tabs and newlines&quot;&gt;
function javascript_escape($str) {
&#160;&#160;&#160;&#160;$new_str = &#039;&#039;;

&#160;&#160;&#160;&#160;for($i = 0; $i &lt; strlen($str); $i++) {
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$val = ord(substr($str, $i, 1));
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$prefix = $val &lt; 16 ? &#039;\\x0&#039; : &#039;\\x&#039;;
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;$new_str .= $prefix . dechex($val);
&#160;&#160;&#160;&#160;}

&#160;&#160;&#160;&#160;return $new_str;
}
&lt;/blockquote&gt;</description>
		<content:encoded><![CDATA[<blockquote cite="This one handles tabs and newlines"><p>
function javascript_escape($str) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;$new_str = &#8221;;</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;for($i = 0; $i &lt; strlen($str); $i++) {<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$val = ord(substr($str, $i, 1));<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$prefix = $val &lt; 16 ? &#8216;\\x0&#8242; : &#8216;\\x&#8217;;<br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$new_str .= $prefix . dechex($val);<br />
&nbsp;&nbsp;&nbsp;&nbsp;}</p>
<p>&nbsp;&nbsp;&nbsp;&nbsp;return $new_str;<br />
}
</p></blockquote>
]]></content:encoded>
	</item>
</channel>
</rss>