PDA

View Full Version : using php to trim a text file


Subwaydesigns
08-04-2004, 10:46 PM
I currently am using two text files that I'm loading into flash using loadVars() - one of them is plain text, the other has html tags. I'm wondering if there would be a way to get rid of one of them, and delete the html tags through php.

Ideally, the php would load the text file with the html tags, and return a variable to the flash which would contain the content of the text file, minus the html tags.

I played around with ereg_replace, but the problem is I don't know how to search for a particular tag inside the string and delete it.

My second question is if I can use php the same way I use textfiles as far as loadVars is concerned. Right now, my code looks like:

loadText = new loadVars();
loadText.load(_root.section.section+".txt");
loadText.onLoad = function(success) {
if (success) {
my_st = this.mytext;
title = this.mytitle;
}
};


Can I do something like loadText.load(_root.section.section+".php"); and then have something like $mytext = "blahblahblah" and $mytitle = "titleblahblah" in the php? or do I have to use LoadVariables (please say no).

Thanks in advance, any advice/information is greatly appreciated.

-S.

my php so far looks like:

<?php
$filename="HOME_o.txt";
$fp = fopen($filename, "r") or die("Error opening file");
$filecontent = fread($fp, filesize($filename));

$phptext = ereg_replace( "&mytitle=HOME&mytexto=(.*)#", "\\1", $filecontent );
$phptitle = "HOME";
?>


and the text file is simply this:

&mytitle=HOME&mytexto=Lorem <u>ipsum</u> dolor sit amet, consectetuer adipiscing elit. Vivamus vel velit. Etiam posuere. Suspendisse potenti. Suspendisse auctor. Duis pellentesque pellentesque ligula. Nunc tincidunt, nulla eu pellentesque convallis, urna elit suscipit tortor, non aliquam nulla elit sed quam. Vestibulum faucibus, turpis sit amet dapibus facilisis, nisl mi euismod diam, quis scelerisque massa dolor eget nunc.
Pellentesque sodales. Nunc interdum massa vel elit. Nullam lectus. Sed quis nibh. Donec volutpat commodo mi. In velit ipsum, porttitor et, vestibulum vel, molestie in, justo. Etiam et libero eget diam mattis tempus. Vestibulum dapibus. Sed dignissim.
Sed commodo risus a nunc. Fusce est. Nullam pharetra libero ac eros. Vivamus lectus. Sed nec urna. Aliquam eu neque. Pellentesque massa lectus, auctor id, posuere eget, malesuada sit amet, arcu. Ut justo. Sed tellus elit, dictum eget, semper vel, dapibus semper, pede. Praesent mattis malesuada pede.
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Vivamus vel velit. Etiam posuere. Suspendisse potenti. Suspendisse auctor. Duis pellentesque pellentesque ligula. Nunc tincidunt, nulla eu pellentesque convallis, urna elit suscipit tortor, non aliquam nulla elit sed quam. Vestibulum faucibus, turpis sit amet dapibus facilisis, nisl mi euismod diam, quis scelerisque massa dolor eget nunc.
Pellentesque sodales. Nunc interdum massa vel elit. Nullam lectus. Sed quis nibh. Donec volutpat commodo mi. In velit ipsum, porttitor et, vestibulum vel, molestie in, justo. Etiam et libero eget diam mattis tempus. Vestibulum dapibus. Sed dignissim.
Sed commodo risus a nunc. Fusce est. Nullam pharetra libero ac eros. Vivamus lectus. Sed nec urna. Aliquam eu neque. Pellentesque massa lectus, auctor id, posuere eget, malesuada sit amet, arcu. Ut justo. Sed tellus elit, dictum eget, semper vel, dapibus semper, pede. Praesent mattis malesuada pede.#

game_programmer
08-04-2004, 11:05 PM
I've done much the same project. Believe me, loadVars is not a fun way to go, it makes me very sad. What I have used, very successfully, is XML, it's much more expandable, and you don't run into stupid URL encoding stuff.

Unfortunately, you cannot read from files directly in Flash. With using loadVars you have to generate a URL encoded string, created from a PHP file like you're doing. I can tell you that you will reach the extent of this very quickly. I only use loadVars when it's just a couple numbers I need to import. Otherwise it's a waste. Another method that is similar to loadVars is pumping the variables directly into the <embed> so you have <embed src="myGFlash.swf&monkey=2&babymonkey=5"> etc...

At that point you can call the variables directly in your root. So I can say:

mytext=monkey

and then mytext will = 2

Hope this helps you out. There's tons of XML examples and tutorials out there for Flash, believe me, it's worth checking out.

Let me know if this helps.

annexion
08-05-2004, 02:53 AM
If you use loadVars.onData you don't have to urlEncode anything. The project I'm current working on loads and send many thousands of lines of CSV data back and forth from the server. You can also load raw text, xml, csv, rtf and many other types of files with flash.

Subwaydesigns
08-05-2004, 11:46 AM
WEll after playing with it for a couple hours i got what I wanted. Using loadVars, actually - I just had php load the text file, make changes to it, and then echo the variables for flash.

Didn't have to URL encode, by the way, I just used


echo "&mytext=$phptext";
echo "&mytitle=$phptitle";


and used loadVars (file.php); in the flash...

Thanks!