<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Saturnboy &#187; regexp</title>
	<atom:link href="http://saturnboy.com/tag/regexp/feed/" rel="self" type="application/rss+xml" />
	<link>http://saturnboy.com</link>
	<description>Code, Work, and Life</description>
	<lastBuildDate>Wed, 28 Jul 2010 13:20:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Parsing Twitter with RegExp</title>
		<link>http://saturnboy.com/2010/02/parsing-twitter-with-regexp/</link>
		<comments>http://saturnboy.com/2010/02/parsing-twitter-with-regexp/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 11:51:02 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[regexp]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=1055</guid>
		<description><![CDATA[I needed a very simple Twitter cache for a project I&#8217;m working on. And I was very happy to trade off some realtime accuracy for reliability. In addition to caching the tweets, I also needed to pre-process them into css-able html with clickable links, usernames, and hashtags. The web had a few nice examples of [...]]]></description>
			<content:encoded><![CDATA[<p>I needed a very simple Twitter cache for a project I&#8217;m working on.  And I was very happy to trade off some realtime accuracy for reliability.  In addition to caching the tweets, I also needed to pre-process them into css-able html with clickable links, usernames, and hashtags.  The web had a <a href="http://www.simonwhatley.co.uk/parsing-twitter-usernames-hashtags-and-urls-with-coldfusion">few</a> <a href="http://www.snipe.net/2009/09/php-twitter-clickable-links/">nice</a> <a href="http://snipplr.com/view/28483/regex-to-make-twitter-links-clickable/">examples</a> of how to use regular expressions to parse the raw tweet text, but I decided to take what I liked and do the rest myself.</p>
<h5>Links</h5>
<p class="bottom">Here&#8217;s the PHP code for parsing links out of the raw tweet text:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@'</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'&lt;a href=&quot;$1&quot;&gt;$1&lt;/a&gt;'</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I only wanted <code>http</code> and <code>https</code> links, with an optional query part <code>(\?\S+)?</code> and an option anchor part <code>(#\S+)?</code>.  The conversion of a text link into an html link is done using back references, which in PHP is <code>$1</code>, <code>$2</code>, etc.  In the expression above, I use <code>$1</code> twice to put the matched link into both the <code>href</code> attribute and the link text.</p>
<h5>Users</h5>
<p class="bottom">Here&#8217;s the PHP code for parsing Twitter usernames:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'/@(\w+)/'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'&lt;a href=&quot;http://twitter.com/$1&quot;&gt;@$1&lt;/a&gt;'</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Nothing special, just take the @ and all following word characters (letters, digits, and underscores), and turn it into a user link.</p>
<h5>Hashtags</h5>
<p class="bottom">Here&#8217;s the PHP code for parsing Twitter hashtags:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'/\s+#(\w+)/'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">' &lt;a href=&quot;http://search.twitter.com/search?q=%23$1&quot;&gt;#$1&lt;/a&gt;'</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Getting the hashtags right was the most tricky of the three.  I decided to only grab hashtags that were proceeded by one or more spaces.  The real magic is the <code>%23</code> in the query string, which forces a search on the complete hashtag, including the <code>#</code> part.  For example, compare a search for <a href="http://search.twitter.com/search?q=%23flex">#flex</a> to a search for <a href="http://search.twitter.com/search?q=flex">flex</a>.</p>
<h5>The Cache</h5>
<p>The cache is just a simple cron job that periodically queries Twitter and retrieves the latest tweets.  Most importantly, the cache fails gracefully if Twitter is inaccessible, which it does by doing exactly nothing if Twitter is down.  This guarantees that my app always has valid data (when my server is up, the cache is up too), but with the possibility that the data is a little old.</p>
<p class="bottom">Here&#8217;s the notable function in the cache:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> getTweets<span style="color: #009900;">&#40;</span><span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$num</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">//first, get the user's timeline</span>
    <span style="color: #000088;">$ch</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_init</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_URL<span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;http://twitter.com/statuses/user_timeline/<span style="color: #006699; font-weight: bold;">$user</span>.json?count=<span style="color: #006699; font-weight: bold;">$num</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_setopt</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #339933;">,</span> CURLOPT_RETURNTRANSFER<span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$json</span> <span style="color: #339933;">=</span> <span style="color: #990000;">curl_exec</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">curl_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$ch</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">//abort on error</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//second, convert the resulting json into PHP</span>
    <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$json</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">//third, build up the html output</span>
    <span style="color: #000088;">$s</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$item</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//handle any special characters</span>
        <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">htmlentities</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">text</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">ENT_QUOTES</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'utf-8'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//build the metadata part</span>
        <span style="color: #000088;">$meta</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'g:ia M jS'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">created_at</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' from '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$item</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">source</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//parse the tweet text into html</span>
        <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'@(https?://([-\w\.]+)+(/([\w/_\.]*(\?\S+)?(#\S+)?)?)?)@'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;a href=&quot;$1&quot;&gt;$1&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/@(\w+)/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;a href=&quot;http://twitter.com/$1&quot;&gt;@$1&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$text</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s#(\w+)/'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' &lt;a href=&quot;http://search.twitter.com/search?q=%23$1&quot;&gt;#$1&lt;/a&gt;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$text</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">//assemble everything</span>
        <span style="color: #000088;">$s</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'&lt;p class=&quot;tweet&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$text</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;span class=&quot;tweet-meta&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$meta</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/span&gt;&lt;/p&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>First, we query the user&#8217;s JSON timeline using <a href="http://curl.haxx.se/">cURL</a>.  Second, we use PHP&#8217;s awesome json_decode function to convert the JSON into objects.  And lastly, we iterate over the tweets and parse everything into our desired HTML output.</p>
<p class="bottom">Here some sample output from <a href="http://twitter.com/saturnboy">my twitter</a> feed:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;p class=&quot;tweet&quot;&gt;Been reading Programming Goggle App Engine. Actually feeling dumber now than before I started. Too much to learn.&lt;br /&gt; 
&lt;span class=&quot;tweet-meta&quot;&gt;2:58pm Feb 14th from &lt;a href=&quot;http://www.tweetdeck.com/&quot; rel=&quot;nofollow&quot;&gt;TweetDeck&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&nbsp;
&lt;p class=&quot;tweet&quot;&gt;Blog Post :: Async Testing with FlexUnit 4 :: &lt;a href=&quot;http://bit.ly/cGLnaI&quot;&gt;http://bit.ly/cGLnaI&lt;/a&gt;&lt;br /&gt; 
&lt;span class=&quot;tweet-meta&quot;&gt;3:33pm Feb 11th from &lt;a href=&quot;http://www.tweetdeck.com/&quot; rel=&quot;nofollow&quot;&gt;TweetDeck&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;
&nbsp;
&lt;p class=&quot;tweet&quot;&gt;Blog Post :: A Better HTML Template for Flex 4 :: &lt;a href=&quot;http://bit.ly/70DLsj&quot;&gt;http://bit.ly/70DLsj&lt;/a&gt;&lt;br /&gt; 
&lt;span class=&quot;tweet-meta&quot;&gt;12:55pm Jan 25th from &lt;a href=&quot;http://www.tweetdeck.com/&quot; rel=&quot;nofollow&quot;&gt;TweetDeck&lt;/a&gt;&lt;/span&gt;&lt;/p&gt;</pre></div></div>

<p>Once I have the output, I can do whatever I want with it: save to disk, stick it in the database, keep it in memory, cache it in <a href="http://memcached.org/">memcache</a>, etc.  In my case, I wanted the simplest possible option, so I chose to write it out as a static html file.</p>
<p>The end.  The rest of the app&#8217;s not ready yet&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/02/parsing-twitter-with-regexp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Special Characters in Flex 4</title>
		<link>http://saturnboy.com/2010/01/special-characters-flex-4/</link>
		<comments>http://saturnboy.com/2010/01/special-characters-flex-4/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 05:07:31 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[flex4]]></category>
		<category><![CDATA[regexp]]></category>

		<guid isPermaLink="false">http://saturnboy.com/?p=926</guid>
		<description><![CDATA[Recently, I was caught by a special characters vs. html entities issue in Flex 4. For reference, you can read more about special characters in the text property of a text component in the official docs. And also here on Flex Examples. Unfortunately, neither of these was exactly what I was looking for. The Problem [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I was caught by a special characters vs. html entities issue in Flex 4.  For reference, you can read more about special characters in the <code>text</code> property of a text component in the <a href="http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf69084-7d53.html">official docs</a>.  And also <a href="http://blog.flexexamples.com/2007/08/02/using-special-characters-in-your-flex-applications/">here</a> on <a href="http://blog.flexexamples.com/">Flex Examples</a>.  Unfortunately, neither of these was exactly what I was looking for.</p>
<h5>The Problem</h5>
<p>I had an array of names in ActionScript that potentially contained special characters, and I wanted to output them in a spark <code>List</code> component.  Nothing magical required, just get them on the screen.</p>
<h5>Solution #1</h5>
<p class="bottom">One option, which I&#8217;ve <b>NEVER</b> seen in anyone&#8217;s code ever, is to move the definition of the array into its own <code>Script</code> tag without the <code>CDATA</code> block.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;</span>fx<span style="color: #000000; font-weight: bold;">:</span>Script<span style="color: #000000; font-weight: bold;">&gt;</span>
    <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> nuggets<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Array</span> = <span style="color: #000000;">&#91;</span>
        <span style="color: #990000;">'Carmelo Anthony'</span>,
        <span style="color: #990000;">'Chauncey Billups'</span>,
        <span style="color: #990000;">'Nen&amp;#234;'</span>,
        <span style="color: #990000;">'Kenyon Martin'</span><span style="color: #000000;">&#93;</span>;
<span style="color: #000000; font-weight: bold;">&lt;/</span>fx<span style="color: #000000; font-weight: bold;">:</span>Script<span style="color: #000000; font-weight: bold;">&gt;</span></pre></div></div>

<p>Now, it doesn&#8217;t matter if we use single quotes (<code>'</code>) or double quotes (<code>"</code>), because outside of the <code>CDATA</code> block all numeric html entities are processed.  The fact that Flash Builder 4 automatically inserts the <code>CDATA</code> block when you open a <code>Script</code> tag probably means that almost no one has ever even heard of this possible solution.  It&#8217;s so weird, I can&#8217;t recommend this solution.</p>
<blockquote class="deeper"><p><b>Digging Deeper:</b> The <a href="http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS2db454920e96a9e51e63e3d11c0bf69084-7d53.html">official docs</a> will tell you that only a few named html entities work (<code>&amp;lt;</code>, <code>&amp;gt;</code>, <code>&amp;amp;</code>, <code>&amp;quot;</code>, <code>&amp;apos;</code>), and after that you must use numeric html entities (<code>&amp;#NNN;</code>).</p></blockquote>
<h5>Solution #2</h5>
<p class="bottom">Another option, that I actually thought of first, is to process the numeric html entities via a regular expression to output the correct special character.  For example:</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> makeSpecialChars<span style="color: #000000;">&#40;</span>item<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">Object</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">return</span> item.<span style="color: #004993;">toString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>.<span style="color: #004993;">replace</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight: bold;">/&amp;</span>#\<span style="color: #004993;">d</span><span style="color: #000000; font-weight: bold;">+</span>;<span style="color: #000000; font-weight: bold;">/</span>g, replaceFunc<span style="color: #000000;">&#41;</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> replaceFunc<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> <span style="color: #000000;">&#123;</span>
    <span style="color: #6699cc; font-weight: bold;">var</span> s<span style="color: #000000; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #004993;">arguments</span><span style="color: #000000;">&#91;</span><span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#93;</span>;
    s = s.<span style="color: #004993;">substring</span><span style="color: #000000;">&#40;</span><span style="color: #000000; font-weight:bold;">2</span>, s.<span style="color: #004993;">length</span> <span style="color: #000000; font-weight: bold;">-</span> <span style="color: #000000; font-weight:bold;">1</span><span style="color: #000000;">&#41;</span>;
    s = <span style="color: #004993;">String</span>.<span style="color: #004993;">fromCharCode</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">parseInt</span><span style="color: #000000;">&#40;</span>s<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>;
    <span style="color: #0033ff; font-weight: bold;">return</span> s;
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>We use a simple regular expression to match any numeric html entity, and then call a replacement function to do the work of converting the entity into a special character.  The static method <code>String.fromCharCode</code> does the actual conversion.</p>
<p class="bottom">Putting the converter code together with a <code>List</code>&#8216;s <code>labelFunction</code> property and we get this:</p>

<div class="wp_syntax"><div class="code"><pre class="mxml" style="font-family:monospace;"><span style="color: #000000;"><span style="color: #7400FF;">&lt;fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;">&lt;!<span style="color: #66cc66;">&#91;</span>CDATA<span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">    <span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> private var nuggets2:Array = <span style="color: #66cc66;">&#91;</span></span>
<span style="color: #000000;">        <span style="color: #ff0000;">'Carmelo Anthony'</span>,</span>
<span style="color: #000000;">        <span style="color: #ff0000;">'Chauncey Billups'</span>,</span>
<span style="color: #000000;">        <span style="color: #ff0000;">'Nen&amp;#234;'</span>,</span>
<span style="color: #000000;">        <span style="color: #ff0000;">'Kenyon Martin'</span><span style="color: #66cc66;">&#93;</span>;</span>
&nbsp;
<span style="color: #000000;">    private function makeSpecialChars<span style="color: #66cc66;">&#40;</span>item:Object<span style="color: #66cc66;">&#41;</span>:String <span style="color: #66cc66;">&#123;</span></span>
<span style="color: #000000;">        ...same as above...</span>
<span style="color: #000000;">    <span style="color: #66cc66;">&#125;</span></span>
<span style="color: #000000;"><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#93;</span><span style="color: #7400FF;">&gt;</span></span>
<span style="color: #000000;"><span style="color: #7400FF;">&lt;/fx:Script</span><span style="color: #7400FF;">&gt;</span></span>
&nbsp;
<span style="color: #000000;"><span style="color: #7400FF;">&lt;s:List</span> dataProvider=<span style="color: #ff0000;">&quot;{new ArrayList(nuggets2)}&quot;</span></span>
<span style="color: #000000;">    labelFunction=<span style="color: #ff0000;">&quot;makeSpecialChars&quot;</span> <span style="color: #7400FF;">/&gt;</span></span></pre></div></div>

<p>For each element in the array, the <code>labelFunction</code> gets called and any numeric html entity is converted into the corresponding special character.  No magic.</p>
<h5>The Result</h5>
<p class="bottom">Here is the final result with both Solution #1 and Solution #2 together (view source enabled):</p>
<div id="flashcontent-special-chars">
Flash is required.  <a href="http://www.adobe.com/go/getflashplayer">Get it here!</a>
</div>
<p>If you view the source, you can see that I&#8217;m using two separate <code>Script</code> tags, one with a <code>CDATA</code> block and one without.  Who does that?</p>
<h5>Files</h5>
<ul>
<li><a href="http://saturnboy.com/proj/flex4/special_chars/SpecialChars.html">SpecialChars</a> (<a href="http://saturnboy.com/proj/flex4/special_chars/srcview/SpecialChars.zip">download</a>)</li>
</ul>
<div>
<script type="text/javascript">
swfobject.embedSWF('http://saturnboy.com/proj/flex4/special_chars/SpecialChars.swf', 'flashcontent-special-chars', '420', '160', '10.0.0', 'playerProductInstall.swf', false, { bgColor:'#ffffff', base:'.' });
</script>
</div>
]]></content:encoded>
			<wfw:commentRss>http://saturnboy.com/2010/01/special-characters-flex-4/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
