rhyspaterson Posted November 27, 2007 Share Posted November 27, 2007 Hey guys, Have a bit of a problem, and am quite unsure of how to fix it. I have a variable that is returned to me that looks like this: Policy=frank; IP Address=10.10.10.10 I want to strip the string and turn it into an array so that i have only 'frank' and '10.10.10.10' (array[0] and array[1] respectively). Any suggestions my elite coders? Cheers! Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 27, 2007 Share Posted November 27, 2007 Here You Go: <script language="javascript"> var Policy="frank"; var IPAddress="10.10.10.10"; var myinfo = new Array(""+Policy+"",""+IPAddress+""); document.write(myinfo[0] + "<br>"); document.write(myinfo[1]); </script> Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted November 27, 2007 Author Share Posted November 27, 2007 Thanks for your reply! Unfortunately i think i may have not been clear enough. I have a single variable with multiple bits of information within it. var foo = 'Policy=frank; IP Address=10.10.10.10'; I would like to strip the foo variable of everything except 'frank' and '10.10.10.10' and split it into an array. Please note that 'frank' could be any name and '10.10.10.10' could be any IP address. I was thinking of something that would search for the = signs, capture everything after it until it reaches a space, then looked for the next = sign and do the same... but i have no idea how to go about it. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 27, 2007 Share Posted November 27, 2007 look into JavaScript search() Method if you use the script I provided along with PHP to get your JavaScript variables and incorporate the search method into this script; it should still work the way you want it to. Quote Link to comment Share on other sites More sharing options...
clearstatcache Posted November 27, 2007 Share Posted November 27, 2007 <html> <script language="javascript"> var str = "Policy=frank; IP Address=10.10.10.10"; var mixed_array = new Array(); var tmp_arr = new Array(); var arr = new Array(); mixed_array = str.split(';'); for(x=0; x<mixed_array.length; x++) { tmp_arr = mixed_array[x].split('='); arr[x] = tmp_arr[1]; } for(x=0; x<arr.length; x++) { document.write("array[" + x + "] -- " + arr[x]+"<br>"); } </script> </html> Quote Link to comment Share on other sites More sharing options...
rhyspaterson Posted November 28, 2007 Author Share Posted November 28, 2007 Thanks for that! Turned it into a function and changed a few things: /* * Clean up the string */ splitStringToArray : function ( string ) { var string = dController.policy; var mixedArray = new Array(); var temporaryArray = new Array(); var array = new Array(); mixedArray = string.split(';'); for(x = 0; x < mixedArray.length; x++) { temporaryArray = mixedArray[x].split('='); array[x] = temporaryArray[1]; } return array; } , Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.