Plug in PHP 100 POWER SOLUTIONS- P15 pps

5 201 0
Plug in PHP 100 POWER SOLUTIONS- P15 pps

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

Thông tin tài liệu

36 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 36 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 Next an if … else pair of tests checks whether, if added together, the current line length, $len, plus the current word length, $wlen, would be less than the required width, $width. If so, then the word is appended to $wrapped, followed by a space, and then $len is updated accordingly. If adding the word to the current line would have made it too long, then the else part of the test is executed. Here any space character previously added to $wrapped is now unnecessary and is removed by a quick call to rtrim(), which removes whitespace from a string’s tail. Then, a <br /> tag followed by a newline character (to help make viewing the page source clearer) and a space are appended to $wrapped, followed by $word (which is now on a new line). The <br /> is used because a \n does not add a line break to HTML output. The value of $len is then updated to reflect this. Once the inner loop has completed executing, rtrim() is again called to remove any extra space added. But it isn’t needed now, so a <br /> tag and newline are appended to $wrapped to signify reaching the end of a paragraph. Once the outer loop has also completed, the text has been fully processed and so the value in $wrapped is returned to the calling code. How to Use It To transform unwrapped text into wrapped, call the function like this: echo PIPHP_WrapText($message, 80, 5); Here $message is the text to be wrapped, 80 is the character at which to force the wrapping, and 5 is the number of characters by which to indent the start of each paragraph. If you don’t want indenting, just set the third parameter to zero. The Plug-in function PIPHP_WrapText($text, $width, $indent) { $wrapped = ""; $paragraphs = explode("\n", $text); foreach($paragraphs as $paragraph) { if ($indent > 0) $wrapped .= str_repeat("&nbsp;", $indent); $words = explode(" ", $paragraph); $len = $indent; foreach($words as $word) { $wlen = strlen($word); if (($len + $wlen) < $width) { $wrapped .= "$word "; $len += $wlen + 1; } else C h a p t e r 3 : Te x t P r o c e s s i n g 37 C h a p t e r 3 : Te x t P r o c e s s i n g 37 { $wrapped = rtrim($wrapped); $wrapped .= "<br />\n$word "; $len = $wlen; } } $wrapped = rtrim($wrapped); $wrapped .= "<br />\n"; } return $wrapped; } Caps Control When dealing with user input, you will often come across people who keep their Caps Lock key permanently enabled, which can make reading what they write difficult on the eye. It also looks like they are shouting. To diminish or entirely remove this problem, use this plug- in, which also supports three other upper- and lowercase text transformations. Figure 3-2 shows these four transformations applied to a poem by Lewis Carroll. FIGURE 3-2 Converting all caps or other nonstandard text to a more readable form using the Caps Control plug-in 2 38 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 38 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 About the Plug-in This plug-in takes a string variable containing any text and transforms its case according to the second parameter. It takes these arguments: • $text A string variable containing the text to be transformed • $type A string containing the type of transformation to make: “u” – Capitalize all letters “l” – Set all letters to lowercase “w” – Capitalize the first letter of every word “s” – Capitalize the first letter of every sentence Variables, Arrays, and Functions $newtext String variable containing transformed text $words Array of all words in the text $word String containing the current word being processed $sentences Array of all sentences in the text $sentence String containing the current sentence being processed How It Works This plug-in is based around a four-way switch statement, the first two elements of which are extremely simple in that if the style of transform requested (passed in the $type variable) is either “u” or “l”, then the text to transform is simply passed through either the strtoupper() or strtolower() functions and then returned. If the transformation type is “w”, then the string variable $newtext is initialized to the empty string; it will be used to build the transformed string to be returned. Then, all the words in the text are extracted into the array $words using the function explode(), which is set to split $text into smaller strings at each space character and return the result in an array. Next a foreach loop iterates through all the elements in $words, placing them one at a time in the string variable $word, from where they are first converted to lowercase using strtolower(), and then the first letter of the word is converted to uppercase using the ucfirst() function. After this, a space is added back to the end of each word. Once $newtext has been constructed, any extra space that was appended is removed using the rtrim() function and the string is returned. If the transformation type is “s”, then $newtext is initialized to the empty string and all the sentences are extracted into the array $sentences using the explode() function. From here they are processed one at a time, using a foreach loop, into the string variable $sentence, which is then converted to lowercase using strtolower(). Any preceding whitespace is removed using ltrim(), and then the first character of the sentence is set to uppercase using the ucfirst() function. After building $newtext, any trailing space is removed and the string is returned. In the case of an unknown type being passed to this function, the final line will return the original string unchanged. C h a p t e r 3 : Te x t P r o c e s s i n g 39 C h a p t e r 3 : Te x t P r o c e s s i n g 39 How to Use It You use the plug-in by calling it up in one of the four following ways: echo PIPHP_CapsControl($text, "u"); echo PIPHP_CapsControl($text, "l"); echo PIPHP_CapsControl($text, "w"); echo PIPHP_CapsControl($text, "s"); The $text argument should contain the string to transform, while the second argument should be one of the four letters shown (in lowercase). The Plug-in function PIPHP_CapsControl($text, $type) { switch($type) { case "u": return strtoupper($text); case "l": return strtolower($text); case "w": $newtext = ""; $words = explode(" ", $text); foreach($words as $word) $newtext .= ucfirst(strtolower($word)) . " "; return rtrim($newtext); case "s": $newtext = ""; $sentences = explode(".", $text); foreach($sentences as $sentence) $newtext .= ucfirst(ltrim(strtolower($sentence))) . ". "; return rtrim($newtext); } return $text; } Friendly Text Sometimes when you have text to post on a web site, it can be quite dry and unexciting. Although there’s not much you can do about that (apart from completely rewriting it), at least you can make it read better by converting it into as friendly a form as possible. This is achieved by using contractions. For example, replacing you have with you’ve or it is with it’s is easier to read and more like the way we speak in everyday life, and this code takes that concept to the extreme. Figure 3-3 shows an excerpt from one of Winston Churchill’s speeches, which now flows a lot better, although I admit, the original has a certain punchiness and power that’s lost in 3 40 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 40 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 the conversion. Still, it shows you can leave this plug-in running on your server and it will almost always produce proper readable English. This is also a good example of why plug-ins are so useful, because you probably could write this code quite easily yourself, but actually sitting down and working out all the various parts of the rules of the English language (and all its exceptions) is quite time- consuming. Thankfully, though, I’ve done all that work for you. About the Plug-in This plug-in takes a string variable containing English text, processes it into a “friendly” form of speech, and returns the modified text. It takes these arguments: • $text A string variable containing the text to be modified • $emphasis A Boolean value which, if TRUE, underlines all modifications FIGURE 3-3 The Friendly Text plug-in is used to convert a famous speech with the underline option enabled for testing. Variables, Arrays, and Functions $misc Array containing pairs of strings to find and substitute $nots Array of words that can preface the word not $haves Array of words that can preface the word have $who Array of pronouns and question words $what Array of common verbs that can be contracted . the Plug- in This plug- in takes a string variable containing any text and transforms its case according to the second parameter. It takes these arguments: • $text A string variable containing. Functions $newtext String variable containing transformed text $words Array of all words in the text $word String containing the current word being processed $sentences Array of all sentences in the text $sentence String. the Plug- in This plug- in takes a string variable containing English text, processes it into a “friendly” form of speech, and returns the modified text. It takes these arguments: • $text A string

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