Great CDATA Trick – (Multiple Line String Literal)

One thing I like about php strings is that you can easily write them on multiple lines:

1
2
3
4
5
6
$theString = "
<!DOCTYPE html>
  <html>
    <head>
      <meta charset='utf-8' /> 
etc....";

It always bugged me that you couldn’t do this in actionscript. Turns out you can – I did some googling the other day in an attempt to find a way – and to my surprise I found this question on stackoverflow note the answer by Nick Sonneveld.

Here’s how it works:

txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();

It’s usually the case that you want to use condenseWhite to remove extra whitespace – however being able to preserve returns and extra spacing may also be desirable.

I used this because I was manually copy and pasting information from an html version of a word doc – clicking a button in an air app I wrote to remove SOME of the html elements and attributes like and class=”MSsomething” and then pasting the final result into some weird timeline based flash templates (that someone else created). This little snippet saved me some serious time. The air app also did a lot of the work with some regular expressions. Converting things like:

<p>.           stuff</p>

to…

<li>stuff</li>

A funny side note: My syntax highlighter did NOT dig this AS syntax, so to get it to show up in a readable way, I wrote some php directly in the post (I have Exec-PHP installed);

1
2
3
4
5
6
7
8
9
10
11
<?php
echo htmlentities("txt.condenseWhite = true;
txt.htmlText = (<![CDATA
Some list elements one of the many poorly implemented html elements in flash...
  <li>one</li>
  <li>one</li>
  <li>one</li>
...nevertheless they can be highly useful with some hacky stylsheet stuff...
]]>).toString();
");
?>


 
 
 

Leave a Reply

Spam protection by WP Captcha-Free