Fabis94 Posted March 28, 2009 Share Posted March 28, 2009 I have this long code put in a string (i used file_get_contents to get it from another page): 223869,1550,30492847 432402,76,1409849 369422,75,1245567 501886,79,1947581 436335,79,1910533 359856,75,1250659 413704,55,177302 396838,73,1078967 690109,67,551412 6972,99,15319606 242629,81,2315289 785464,64,435123 287781,68,611815 349404,61,318931 786534,52,132517 1139395,60,274476 320949,48,86768 182000,61,305119 264238,56,199676 330524,56,188663 205315,50,102585 232534,53,147552 289503,56,185844 131858,57,205193 180331,49,91820 -1,-1 -1,-1 -1,-1 422632,500 And i need to put each of the parts that parts are divided by commas (1st: 223869, 2nd: 1550 etc.) in an array variable (for example, array[0] is the first part, array[1] is the second one). How do i do this? Im only a PHP beginner at the moment ??? Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/ Share on other sites More sharing options...
wildteen88 Posted March 28, 2009 Share Posted March 28, 2009 Use explode $contents = file_get_contents('your-file-with-codes.txt'); $codes = explode(',', $contents); Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795832 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 So $codes will become an array and when i write (for example) $codes[1] it will return the first part? Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795839 Share on other sites More sharing options...
jackpf Posted March 28, 2009 Share Posted March 28, 2009 Arrays start at 0. $codes[0] will give you the first part... Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795851 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 Is there a way to make explode divide parts by commas OR empty spaces? In the long code there are parts like: 223869,1550,30492847 432402 And after 30492847 is a space not a comma, but those 2 numbers need to be divided too. I was thinking that before i use explode on the $contents array, i replace the spaces with a comma. Is there a function in PHP that changes a character to something else? Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795905 Share on other sites More sharing options...
jackpf Posted March 28, 2009 Share Posted March 28, 2009 str_replace or preg_replace I personally prefer preg_replace Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795920 Share on other sites More sharing options...
wildteen88 Posted March 28, 2009 Share Posted March 28, 2009 Use $contents = file_get_contents('your-file-with-codes.txt'); $codes = explode(',', str_replace(' ', ',', $contents)); Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795935 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 Weird, it still doesn't remove the spaces. Anyway heres an example from where im taking the code: http://hiscore.runescape.com/index_lite.ws?player=Destructo14 I checked it source and it looks like after a part of the code ENTER is pressed and it goes to a new line (it only shows like that in the source so he didn't use <br> or <p></p> What should i do to remove the places where ENTER was pressed/spaces (?) Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795953 Share on other sites More sharing options...
Fabis94 Posted March 28, 2009 Author Share Posted March 28, 2009 nevermind instead of a Space i used chr(10) (/n) and then it worked. EDIT: Sorry i couldn't edit the last post. Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795958 Share on other sites More sharing options...
jackpf Posted March 28, 2009 Share Posted March 28, 2009 If you just use (\s) in preg_replace it'll replace all white space. That's why I prefer it Quote Link to comment https://forums.phpfreaks.com/topic/151523-adding-parts-of-a-string-to-an-array/#findComment-795969 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.