1 00:00:00,160 --> 00:00:02,590 Let's say that we wanted to list the territories and 2 00:00:02,590 --> 00:00:06,330 the armed forces, right along with the other states in alphabetical order. 3 00:00:07,330 --> 00:00:10,037 We'll add a new file named combined_sort. 4 00:00:18,650 --> 00:00:22,499 We'll start by creating a master array of all the options. 5 00:00:25,477 --> 00:00:29,880 We'll use the array merge function to merge each of the files we want to use. 6 00:00:35,558 --> 00:00:40,276 Then we'll use the file function to retrieve each line as a separate 7 00:00:40,276 --> 00:00:41,527 item in an array. 8 00:00:47,684 --> 00:00:50,683 I'm going to add an option to the file function, 9 00:00:50,683 --> 00:00:54,356 that tells the function to ignore the new line characters. 10 00:01:01,306 --> 00:01:04,867 This will trim the new line characters from each of our elements, so 11 00:01:04,867 --> 00:01:08,752 that we can keep things clean, and only add line breaks where we specify. 12 00:01:20,564 --> 00:01:26,441 Territories, And armed forces. 13 00:01:29,086 --> 00:01:31,320 Next, I want to sort these items. 14 00:01:31,320 --> 00:01:33,610 But I want to sort them by the display name, 15 00:01:33,610 --> 00:01:36,680 without looking at any of the HTML tags. 16 00:01:36,680 --> 00:01:40,112 I'm going to create a function to compare the values without tags. 17 00:01:43,058 --> 00:01:44,206 CompareStrings. 18 00:01:48,584 --> 00:01:50,855 We pass the strings we're going to compare, 19 00:01:55,995 --> 00:01:59,042 And then we use the string case compare. 20 00:02:01,783 --> 00:02:05,960 This is a binary safe case-insensitive string comparison. 21 00:02:07,140 --> 00:02:16,128 Then we can use the strip_tags function, On both our values. 22 00:02:21,294 --> 00:02:24,430 This will just leave the display text to compare. 23 00:02:25,450 --> 00:02:27,240 We can then return these results. 24 00:02:28,520 --> 00:02:35,720 Now I can use the user sort function, usort, we pass the array states, 25 00:02:37,450 --> 00:02:42,630 and then we pass the function that we want to use, compareStrings. 26 00:02:44,690 --> 00:02:49,340 This will sort our states array, using our compareStrings function. 27 00:02:49,340 --> 00:02:51,291 Now that our array is ready to go, 28 00:02:51,291 --> 00:02:55,212 we can write it directly to a file using the file_put_contents. 29 00:02:59,385 --> 00:03:05,426 File_put_contents uses the same write mode that we used in the last file with fopen, 30 00:03:05,426 --> 00:03:10,388 but we can also specify a file append flag if we prefer to add the contents 31 00:03:10,388 --> 00:03:12,559 to the end of an existing file. 32 00:03:22,849 --> 00:03:28,276 We use implode to join our array as a string, 33 00:03:28,276 --> 00:03:32,688 and use PHP_EOL as the separator. 34 00:03:37,435 --> 00:03:41,070 This will give us a nice line break between each option. 35 00:03:41,070 --> 00:03:43,516 Let's add a select, and include this new file so 36 00:03:43,516 --> 00:03:45,434 that we can preview it in the browser. 37 00:04:02,330 --> 00:04:04,290 Let's use sorted instead of combined. 38 00:04:08,100 --> 00:04:09,890 Then it won't overwrite the other file. 39 00:04:24,051 --> 00:04:28,749 Now I have a dropdown that includes states, territories, and armed forces, 40 00:04:28,749 --> 00:04:30,470 all in alphabetical order. 41 00:04:32,010 --> 00:04:36,913 There are a lot more functions, feature, and options when it comes to reading and 42 00:04:36,913 --> 00:04:37,917 writing files. 43 00:04:37,917 --> 00:04:42,090 But until you're actually going to use them, they won't do you much good. 44 00:04:42,090 --> 00:04:46,230 Scan through the documentation to get an idea of what is available, so 45 00:04:46,230 --> 00:04:48,340 that when you do need to use it, you're ready. 46 00:04:49,420 --> 00:04:51,350 Now that you know the basics of reading and 47 00:04:51,350 --> 00:04:56,520 writing files with PHP, let's add a parsing component in the next section. 48 00:04:56,520 --> 00:05:00,250 We'll use some of the most common data types, and show how PHP 49 00:05:00,250 --> 00:05:04,980 can parse those files to create a data driven, personal recommendation site.