|
#1
|
|||
|
|||
|
Hi,
Im fairly new to javascript etc so please be patient! Im have a text file which I need to split into strings, and then enter these strings into an arrray. Thus, I have something like this: 1 hello 2 how are you 3 goodbye I want to split this into strings (with each line being a new string) and turn this into the following: a[0] = 'hello' a[1] = 'how are you' a[2] = 'goodbye' Once in the array, the numbers in the original text become unimportant. Now I know that this can be achieved with an arraylist and possibly a scanner(?). But I dont know the syntax to implement these constructs. Any help would be much appreciated! If you cant help with this particular issue, general help on how to put a string into an array would also be useful. Thanks! |
|
#2
|
||||
|
||||
|
Javascript will not (by itself) read a text file. Ajax can possibly be used for that or, if it is on the server, server side scripting.
It is unclear to me what you want to do vs. what you want the script to do. Could you supply more specific information about your project? In any case, the javascript method split() would most likely come in handy. It's syntax is: string.split('char'); Where string is a literal quoted value or a variable containing the string value. The char you use would be a character that you want the string to be split on. For example: Code:
myArray='1 hello,2 how are you,3 goodbye'.split(',');
Code:
myArray[0]='1 hello'; myArray[1]='2 how are you'; myArray[2]='3 goodbye';
__________________
WWWWWWWWWWWW - John________________________ Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate Last edited by jscheuer1; 12-09-2006 at 04:06 AM. |
|
#3
|
|||
|
|||
|
Hi,
Thanks for your reply. Basically I am trying to get a program to accept a variable (a word), and then read through a string and if the variable is in the string, to highlight it. Im using PHP to get the variables etc. In fact the majority of the program uses PHP. Thanks for any help! Last edited by andyl007; 12-11-2006 at 03:58 PM. |
|
#4
|
||||
|
||||
|
It could be something like:
HTML Code:
<script type="text/javascript"> myArray = <? $_GET['filename'] ?>.split(','); </script> Notes: PHP, in its various incarnations, uses different shorthand to tell the server when to turn on and off the PHP interpreter. In my above example, I used the simplest and most common that I am aware of: <? do PHP stuff here ?> For more specifics, you could try asking about this in the PHP forum.
__________________
WWWWWWWWWWWW - John________________________ Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate |
|
#5
|
||||
|
||||
|
John, your PHP will work, but won't output the result, and if it did would output newlines literally, breaking the Javascript. The script would be broken anyway, however, since no quotes surround the string.
You probably intended to use the <?=expr?> shorthand, which outputs the result of expr. However, since it can't be certain that the server in question will have any form of shorthand enabled, the full syntax, <?php statements ?>, should be used whenever writing scripts for other people (or scripts one might wish to move to another server in the future). Code:
<script type="text/javascript">
var myArray = "<?php echo str_replace("\n", '\n', $_GET['filename']); ?>".split(',');
</script>
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP! Last edited by Twey; 12-09-2006 at 04:14 PM. |
|
#6
|
||||
|
||||
|
Quote:
PHP Code:
__________________
WWWWWWWWWWWW - John________________________ Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate |
|
#7
|
||||
|
||||
|
No. Double quotes do interpolation; single quotes don't. So, I'm replacing a newline with the literal string, \n, which will then be parsed by Javascript.
__________________
Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP! |
|
#8
|
|||
|
|||
|
Yeah I probably should have specified that I am running this in PHP.
Thanks, Andy. |
|
#9
|
||||
|
||||
|
Quote:
Does this mean that you now have what you were looking for?
__________________
WWWWWWWWWWWW - John________________________ Really Show Your Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate |
|
#10
|
|||
|
|||
|
So are you are saying that the following code:
Code:
$newarray = split("\n", ',', $_GET['filename']);
Thanks for your help, Andy. Last edited by andyl007; 12-11-2006 at 03:58 PM. |
![]() |
| Bookmarks |
| Thread Tools | Search this Thread |
|
|