Flash special characters with html formatting
Load special characters like & in flash using XML functionality with html formatting.
Special characters like & create chaos when loaded in flash dynamically. Here I will explain how to load special characters in flash in a hassle free manner.
We will use XML to load data from DB with PHP as flash XML holding capabilities are awesome.
Data from DB -> PHP -> XML -> Flash
You will
- Use XML ‘CDATA’ that will allow special characters to be written inside the tag
- Develop a function to filter special characters and reconstruct then in actionscript
Let’s say you have an XML tag that will contain some text data loaded from the DB, first you need to put any content in the data between a CDATA block, a CDATA block is written like this <![CDATA[my text]]>.
Flash’s html handling capability is outstandingly poor. And will probably not read any special characters, and the reason is because it will transform it again, converting this & to this: && making it totally unreadable. There’s a solution, and that is to filter those special chars and re-construct them in ActionScript. I will explain with an example below:
Click here to view the example in which I am displaying data from DB through XML in flash with PHP and MYSQL. And now on the same example I will use the above to display special characters.
Flash part
//rebuildHTML filters special characters and reconstruct in actionscript
function rebuildHTML(theText:String):String {
var tempText:String = theText;
var chunks:Array = new Array();
chunks = tempText.split(“<”);
tempText = chunks.join(“<”);
chunks = tempText.split(“>”);
tempText = chunks.join(“>”);
chunks = tempText.split(“&”);
tempText = chunks.join(“&”);
chunks = tempText.split(“href="”);
tempText = chunks.join(“href=\”");
chunks = tempText.split(“">”);
tempText = chunks.join(“\”>”);
return tempText;
}
myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = function(success) {
if (success) {
// extract XML Data
// myRecords will be an array XML records
myRecords = myXML.firstChild.childNodes;
// Get Number of Records
totalRecords = myRecords.length;
_root.createEmptyMovieClip(“mcMain”,0);
mcMain._x = 20;
mcMain._y = 30;
// loop thru each record
for (ctr = 0; ctr < totalRecords; ctr++) {
// get data from array element
var articleText = myRecords[ctr].firstChild;
currentArticle = mcMain.createEmptyMovieClip(“mcArticle”+ctr, ctr);
if (ctr > 0) {
currentArticle._y = mcMain["mcArticle"+(ctr-1)]._height + mcMain["mcArticle"+(ctr-1)]._y;
}
// create textbox for Article Text
metrics4 = myFormat.getTextExtent(articleText,400);
currentArticle.createTextField(“txtText”, 4, 0, 10, 400, metrics4.textFieldHeight);
currentArticle.txtText.multiline = true;
currentArticle.txtText.html = true;
currentArticle.txtText.htmlText = rebuildHTML(articleText.toString());
}
} else {
trace (“Problem loading XML”);
}
}
myXML.load(“http://localhost/work/poetry.php);
poetry.php
<?php
$link = mysql_connect("localhost","root","");
mysql_select_db("work");
$sql = “SELECT * FROM news ORDER BY id”;
$result = mysql_query($sql) or die(mysql_error());
header(“Content-type: application/xml”);
?>
<news>
<?php
while($row = mysql_fetch_array($result)) :
?><article id=”<?=$row['id']?>”><![CDATA[<?=$row['text']?>]]></article>
<?php
endwhile;
?>
</news>
This is just a section and will not probably work when you copy paste, so I have attached the whole thing on the top of this page download it and work on it. But make sure you test it on a browser
This is not a tutorial on XML flash, if you need a basic tutorial I advise you to visit kirupa.com