Create JS Array from Text File

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobvaz
Forum Newbie
Posts: 6
Joined: Wed Dec 06, 2006 9:19 am

Create JS Array from Text File

Post by bobvaz »

Burrito | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello - a newbie--
I am at a loss as to why the following code will not work...
Commenting out the php results in the coded displays.  uncommenting the the php results in a blank page only.....Help, Please.

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>aaaaaa</title>
</head>
<body>
<script type="text/javascript">
var jsarr= new Array();
<?php 
$filename="linksfile.txt"; 
$lines = file("linksfile.txt"); 

foreach ($lines as $xxx) {
?>
jsarr[jsarr.length]='<?php echo $xxx?>';
<?php	
}
?>
document.write("This is just a check")
document.write(jsarr[0])
</script>
</body>
</html>

Burrito | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

does your linksfile.txt file contain any single quotes?
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

also, file() sometimes adds a line break at the end of each element of the array. my bet is that your output looks something like this:

Code: Select all

jsarr[jsarr.length] = 'whatever your line is here
';
if that's the case, js will choke and die on it. that can easily be fixed with an str_replace().

check your source to find out.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

an array_map() + implode() could probably do it pretty easily after file().
Post Reply