...

View Full Version : Parse txt (or xml) - output to var & call later in page



Barriestard
03-27-2007, 07:25 PM
Hi, Im stuck parsing a document into lines and then echo'ing them out via a variable to certain lines of html code.

IE:

I have in the header:

<?PHP
$textfile= "comments.txt";
$handle = fopen($textfile, "r");
$contents = fread($handle, filesize($textfile));
fclose($handle);
$array01 = explode("&", $contents);
echo $array01[1];
?>

the txt file "comments" contains (for this example):

&ONE
&TWO
&THREE

.. so far I can simply echo out either line one two or three via the array denoted by "&" .. great. Now I need to set this so I can call this echo in certain places of my html code. The text will be pulled in and placed by choice for ALT (im using overlib) text on mousover. So far I have:

<a href="javascript:sendEvent('playitem',0)" onmouseover="return overlib('<?php echo $array01[1];?>', CAPTION, '(click to listen)', STATUS, 'click to listen', FGCOLOR, '#d8e0e3', BGCOLOR, '#175b7b', BORDER, 3, CAPTIONFONT, 'Verdana', TEXTFONT, 'Verdana', TEXTSIZE, 1, ol_width=400, ol_offsetx=12, ol_offsety=17);" onmouseout="return nd();">

(the rest of the line continues using overlib commands)

this in my very basic PHP mind should show "ONE" in my mouseover, but it just causes a JS error in the browser "unterminated string constant".

I know this is basic .. im new to PHP so I may be causing a red herring.

I simply want a text file or xml than can be parsed, split into lines that I can ID and echo out into the html line you see above so each line of text on mouseover via overlib is the text from the document per line.

I see examples where its echoed out to a table etc but I need it to be called/echoed within my specific html line using a variable, which is where I get stuck.

all help welcomed, example code and description would be great :)

skilled360
03-28-2007, 04:37 PM
Hey Barriestard, I'm no guru so don't quote me on this but maybe you should try using

<?print_r($array01[1]);?>
im not so sure that yuo can just echo arrays that way. but i could be wrong. Let me know

aedrin
03-28-2007, 04:53 PM
this in my very basic PHP mind should show "ONE" in my mouseover, but it just causes a JS error in the browser "unterminated string constant".

Load the PHP file in your browser, right click the page and choose View Source.

Copy the JavaScript line and post it on here. Perhaps you are getting a warning.


im not so sure that yuo can just echo arrays that way. but i could be wrong. Let me know

The print_r() function is only useful for debugging purposes. And it only works good on arrays. So using print_r() on a single element from a 1 dimensional array wouldn't prove useful. It is perfectly fine to echo the single element with echo or print.

Your code appears to be correct (at a glance). I do recommend naming your variables to something more logical than array01.

skilled360
03-28-2007, 06:32 PM
The print_r() function is only useful for debugging purposes. And it only works good on arrays. So using print_r() on a single element from a 1 dimensional array wouldn't prove useful. It is perfectly fine to echo the single element with echo or print.


ohh i never knew it was only useful in debugging. The only times i've needed to use it so far is debugging so I hadn't found out that it doesn't work for the uses I was talking about...
Thanks

Fumigator
03-28-2007, 06:58 PM
Well if you send print_r() a string or an integer or a float, then yeah it acts just like print or echo, so I guess it's ok to use... it's just more "correct" to use echo or print. print_r as aedrin mentioned gives you extra information about an array or an object which wouldn't be of any interest to your end-user but is very useful to us as the developer.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum