| Creating Your First Template Driven PHP Website Part 2 |
|
|
| Written by Handy PHP Administrator | |
| Wednesday, 25 April 2007 | |
|
Page 2 of 2 Until now, the PHP used has been pretty basic but the use of functions like file_get_contents, array, and preg_replace makes the code much more complicated and the use of these functions need to be discussed. file_get_contents is used read the contnets of a file without the burden of using the file system and also allows for reading remote file contents. For example, using file_get_contents you can read any webpage on the Internet. Doing this, you will actually see the page the same way your web browser sees it, in raw HTML. So if you read a remote PHP file, you will only get it's output, not the actual source of the script. Here the function is used to assign the contents of the template.php file to the variable $template. array is a commonly used function in PHP. You can think of an array like an multi-part variable. It will allow you to store many values in a single variable. Along with this function is several related functions which can help you sort the values in an array among other things. In this situation, we use 2 arrays for the search and replace system needed for the keywords. The 2 arrays are related to each other so the order of the first array must match the order of the second array. Some functions can cycle through the values in an array and apply it's command on each. Which brings us to the last function. preg_replace is a replace function that uses regular expressions to identify a pattern in the string. A regular expressioncan be very simple or very complex depending on what you want to match. If you know exactly what you are looking for and that it will nevery vary in any way, the regular expression will be very simple but if you are looking for something that may vary greatly, the regulare expression will be very complex. A simple regular expression would be to find a specific word in a paragraph but a complex expression would be used to find every link on any website! Since the link will vary in position, title, url, extra parameters, and author style; you have to teach the script how to find a link. The preg_replace will look for the search term, replace it with the replace term, based on the string in the third argument. In our situation, the string is represented by the variable $template and the search and repace terms are represented by their repected arrays. This method can also be used to rewrite all of your links to something cleaner or filter out dirty words! With a little planning and some hard work, you could convert your entire HTML website into a template driven PHP system. In the next part, we'll look into data files and query string urls which will convert this static system into an generated on the fly dynamic system. Enjoy your new template driven PHP site. Discuss this article on the forums. (8 posts) |
|
| Last Updated ( Wednesday, 25 April 2007 ) |
| < Prev | Next > |
|---|









