Plug in PHP 100 POWER SOLUTIONS- P14 pot

5 249 0
Plug in PHP 100 POWER SOLUTIONS- P14 pot

Đang tải... (xem toàn văn)

Thông tin tài liệu

C h a p t e r 2 : U s i n g t h e P l u g - i n s 31 C h a p t e r 2 : U s i n g t h e P l u g - i n s 31 On the other hand, there are times when you don’t want to only include PHP program code. For example, sometimes you may have a fixed set of HTML tags and/or text that you use in various places on a web site. In which case, it’s a simple matter to write them once, save them in a file, and just include them as and when needed. Inserting HTML Let’s take the case of a very simple HTML file called file.html, with the following contents: <h1>This is a headline</h1> <p>This is some body text</p> This is not a complete HTML file with document headings and so on. Rather, it’s a snippet containing just a couple of tags of HTML to include, which can be done by altering Example 1, adding a require statement as follows: <?php // Example 2: Including an HTML file $fred = "X"; test(); require "file.html"; echo "Test 2: [fred: $fred] [jane: $jane]<br />"; function test() { $jane = "X"; global $fred; echo "Test 1: [fred: $fred] [jane: $jane]<br />"; } ?> The output from running this code can be seen in Figure 2-2. Now you may be wondering what a require statement was doing in this code when I strongly advised to only use require_once for including PHP files. Well, the reason is because PHP code is not being included here. Rather, some HTML tags and text are being inserted into the program at the current position, and it’s possible that we may wish to include it more than once. If require_once had been used, then it could only be inserted a single time. FIGURE 2-2 Inserting an HTML file into a program 32 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s In addition to immediately including an HTML file within your program, you can also assign its contents to a variable, as in the following statement which loads the contents of file.html into the string variable $html (something you might need to do prior to modifying and/or displaying the HTML): $html = require "file.html"; Another use for including HTML this way could be for pulling in advertisements at the right locations. When you do this, it means you can change advertisements as frequently as you like by simply editing the included file and not touching your program code; this technique is especially useful for advertising programs such as Google AdSense. Including PHP Files from Other Servers You can also include a PHP file from another server, as long as that server is configured to return the PHP file itself—and not execute it and return the result of doing so. To do this, enter the full URL to the file like this: require "http://otherserver.com/functions.php"; However, you must be aware that if you can include such a program file over the Web, then so can anyone else, meaning they can also view its contents, which creates a potential security risk. It’s much better to make sure you store all PHP files you will be needing on your own server. It also means that if the other site is ever down, your program will not be affected. CAUTION Even if you are certain that there is no current risk with a remote PHP file, you should be aware that such a file is at the control of the remote server’s administrator and, if that’s not you, then it could be changed or compromised at any time without your say so. Now that you have a means to easily include the plug-ins in this book within your programs, you’re all set to go. The remaining chapters explain how they work (so you can modify them as necessary), and how to use them, and provides plenty of examples. A handy breakdown of all variables, arrays, and functions used by each is also included. CHAPTER 3 Text Processing 34 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s 34 P l u g - i n P H P : 1 0 0 P o w e r S o l u t i o n s A lthough many web sites have video and other multimedia capabilities, the most fundamental part of almost all web sites remains the information contained within its text. This first batch of plug-ins concentrates on providing a range of functions to facilitate manipulating and presenting text in the most suitable way. Whether you wish to control word wrapping, use of upper- and lowercase, spelling and grammar, text length, unwanted words and characters, or other textual features, there’s a plug-in here that will do the job. Some of these plug-ins are so useful they are themselves used by other plug-ins in this book. Wrap Text You can make text wrap in a browser in various ways, including using tables, iframes, and textareas, but sometimes you need absolute control over the wrapping in terms of the number of characters at which the wrap should occur, regardless of whether the user resizes their browser window. Using this plug-in, it’s easy to pass a string of text and have it wrapped using <br /> tags. What’s more it can also indent the start of each new paragraph by an amount of your choosing. Figure 3-1 shows the opening paragraphs of Charles Dickens’ Oliver Twist, with a wrap width of 71 characters and a paragraph indent setting of 5 characters. FIGURE 3-1 Setting text to wrap at a fixed width is a breeze with the Wrap Text plug-in. 1 C h a p t e r 3 : Te x t P r o c e s s i n g 35 C h a p t e r 3 : Te x t P r o c e s s i n g 35 About the Plug-in This plug-in takes a string variable containing any text and adds <br /> and &nbsp; tags in the right places to make the text wrap and indent paragraphs. It takes these arguments: • $text A string variable containing the text to be wrapped • $width An integer representing the character at which to force word wrapping • $indent An integer representing the number of characters by which to indent each paragraph start Variables, Arrays, and Functions $wrapped String variable containing the wrapped text to be returned $paragraphs Array containing the separate paragraphs as determined by \n characters $paragraph String containing an individual paragraph being processed $words Array of all words in a paragraph $word String containing the current word being processed $len Numeric variable containing the length of the current line $wlen Numeric variable containing the length of the next word to be processed How It Works The plug-in works by first splitting the text passed to it into separate paragraphs using the PHP explode() function with an argument of \n, which is the newline character. What this function does is return an array of substrings based on splitting the original string each time a \n is encountered. The function returns these paragraphs in the array $paragraphs. A foreach loop is then entered passing $paragraphs as the input, and then each iteration of the loop places one paragraph at a time into the string variable $paragraph. NOTE Notice the singular form of the variable name $paragraph, with no “s” on the end. This is a convention I use throughout this book—the plural form of a name being for an array, and the singular form of the same name used for an element extracted from that array. Next a check is made to see whether paragraphs must be indented. If so, $indent will have a value greater than zero and so the str_repeat() function is used to add $indent number of non-blank spaces to the string $wrapped, which contains the wrapped text to be returned. Now it’s time to extract all the words in the current paragraph by using the explode() function again, but this time splitting the text up at each space. The resulting list of wor ds is placed in the array $words. Then, before proceeding into processing the words, the variable $len, which monitors the length of the current line, is set to whatever value $indent has, so that the length of the first line is correctly initialized. Another foreach loop is now used to iterate through the words, assigning each element in the array $words in turn to the string variable $word. Then, the first action taken in the loop is to make a note of the length of the word in the variable $wlen. . as determined by characters $paragraph String containing an individual paragraph being processed $words Array of all words in a paragraph $word String containing the current word being processed $len Numeric. sites remains the information contained within its text. This first batch of plug- ins concentrates on providing a range of functions to facilitate manipulating and presenting text in the most. job. Some of these plug- ins are so useful they are themselves used by other plug- ins in this book. Wrap Text You can make text wrap in a browser in various ways, including using tables, iframes,

Ngày đăng: 07/07/2014, 08:20

Mục lục

  • Contents

  • Acknowledgments

  • Introduction

  • 1 Building a Development Server

    • Windows XP, Windows Vista, and Windows 7

      • Reinstalling Zend Server CE

      • Upgrading Zend Server CE

      • Windows Security Alerts

      • After Installation

      • Uninstalling

      • Document Root

      • Ubuntu and Debian Linux

        • Uninstalling

        • After Installation

        • Document Root

        • Fedora, RHEL, and CentOS Linux

          • Installing MySQL

          • Uninstalling

          • Document Root

          • Other Versions of Linux

            • Installing MySQL

            • Uninstalling

            • Document Root

            • Mac OS X 10.4 Plus on Intel Chips

              • Document Root

              • Uninstalling

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan