1. Trang chủ
  2. » Công Nghệ Thông Tin

Plug in PHP 100 POWER SOLUTIONS- P17 pot

5 211 0

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 348,77 KB

Nội dung

46 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 46 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 the text to process and an array containing words to be highlighted, as well as a parameter defining the type of highlighting. These are the arguments: • $text A string variable containing the text to be modified • $matches An array containing words to highlight • $replace A string representing the action to perform on matching words. If it is any of “u”, “b”, or “i”, then matching words will be highlighted using one of the following: underline, bold face, or italic; otherwise, matching words ar e replaced with the contents of $replace. Variables, Arrays, and Functions $match String containing the current word being matched How It Works The plug-in starts iterating through the $matches array of supplied words one at a time, using a switch statement to decide whether any matches found should be highlighted in underline, bold, or italic font (if $replace contains one of “u”, “b”, or “i”). In the case of highlighting a word, the preg_replace() function is called, passing the following three elements to it: 1. ([^\w]+) looks for any sequence of one or more non-word characters, followed by 2. ($match) … the current word being matched, followed by 3. ([^\w]+) … another sequence of one or more non-word characters Using this pattern, it’s possible to extract individual words by checking for one or more non-word characters on either side of the 2nd parameter ($match). The brackets enclosing each of these parts tell PHP to save the matches found for use in the replace part of the function, where they can be inserted using the values $1, $2, and $3, each representing the values in the order they appear in the brackets. When a match is found, the replace string inserts the non-word characters before the match ($1), followed by <$replace>, which will be one of <u>, <b>, or <i>, followed by the word found ($2), followed by </$replace> to close the tag that was opened, finally followed by the non-word characters after the match ($3). In the case of a string of text having been passed in $replace, rather than one of “u”, “b” or “i”, the same initial match is made except that $match doesn’t have brackets around it because we won’t be needing to save a copy of the match, as it will be replaced. Therefore, the replace section is simpler in that it just replaces the entire match with the value in $replace. How to Use It To use this function, you should provide the text to be checked, an array of words to match, and a string to either replace or highlight matched words. For example, to underline a given set of words, you could use the following line of code: echo PIPHP_WordSelector($text, array("cat", "dog"), "u"); C h a p t e r 3 : T e x t P r o c e s s i n g 47 C h a p t e r 3 : T e x t P r o c e s s i n g 47 If the list of words is long, you probably would not want to create an array on the fly and instead would pre-populate an array first, using code such as these two lines: $words = array("rat", "fish", "cat", "dog", "rabbit"); echo PIPHP_WordSelector($text, $words, "u"); To blank out or censor a set of words, you specify a replace string that is none of “b”, “u”, or “i”. For example, the following line replaces all the words in the array $words that are found in $text with four asterisks: echo PIPHP_WordSelector($text, $words, "****"); The Plug-in function PIPHP_WordSelector($text, $matches, $replace) { foreach($matches as $match) { switch($replace) { case "u": case "b": case "i": $text = preg_replace("/([^\w]+)($match)([^\w]+)/", "$1<$replace>$2</$replace>$3", $text); break; default: $text = preg_replace("/([^\w]+)$match([^\w]+)/", "$1$replace$2", $text); break; } } return $text; } Count Tail Displaying a date in the format “23 November” or “March 12” isn’t really that friendly, and you may wish to use the better flowing “23rd November” and “March 12th”. In fact, there are many places where you use numbers, and they would look better displayed with one of “st”, “nd”, “rd”, or “th” following, such as in the sentence “You’re our 124,362nd” visitor, rather than “You are visitor 124,362,” and so on. Figure 3-6 shows using this plug-in to add the correct suffix to all the numbers between 0 and 100. 6 48 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 48 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 number as input and then returns that number with a suffix of one out of “st”, “nd”, “rd”, or “th”. It takes a single argument: • $number The number on which to append a suffix Variables, Arrays, and Functions $nstring String variable created from $number $pointer Numeric variable that points into $nstring $digit Single character string extracted from $nstring $suffix String representing the suffix to append How It Works In order to operate on individual digits of the given number, it is first turned into a string using the cast keyword (string) and stored in $nstring. This is because, although PHP is a loosely typed language and does its best to automatically change the type of a variable according to how it is accessed, it cannot be relied upon to make the change correctly in this instance, where numbers would have to be treated as strings, which are then treated as arrays. Thus, the forced change of type using the cast statement. Next the numeric variable $pointer is defined with a value derived from the length of $nstring - 1. This means it will also point at (or index into) the final character in $nstring. Using $pointer, the variable $digit is then set to the value of the final digit in the number. The string variable $suffix is then set to the default value “th”, the most common suffix. With all the variables initialized, a test is made to see whether $pointer has a value of 0. In other words, is $number a single-digit number less than 10? A second part of the test then takes the case of $pointer being greater than zero (therefore, $number is 10 or higher), and if it is, tests whether the second to last digit is not the number 1. FIGURE 3-6 Using this plug-in makes it easy to add “st”, “nd”, “rd”, and “th” automatically to numbers. C h a p t e r 3 : T e x t P r o c e s s i n g 49 C h a p t e r 3 : T e x t P r o c e s s i n g 49 The reason for this test is that any number ending in 1, 2, or 3 usually requires the suffix “st”, “nd”, or “rd” unless the previous digit is a 1, in which case the suffix must be “th”, as in 11th, 12th, and 13th. If it isn’t an exception case, the switch statement sets $suffix to one of the three lesser common suffixes if the last digit is a 1, 2, or 3. Otherwise, you will recall, $suffix was already set to “th” by default. Finally, the number is returned with the correct suffix appended. How to Use It To add a suffix to a number, just call the plug-in by passing the number, like this: echo PIPHP_CountTail(123); So, for example, to create the output shown in Figure 3-6, you could use the following: for ($j = 0 ; $j < 101 ; ++$j) echo PIPHP_CountTail($j) . ", "; The Plug-in function PIPHP_CountTail($number) { $nstring = (string) $number; $pointer = strlen($nstring) - 1; $digit = $nstring[$pointer]; $suffix = "th"; if ($pointer == 0 || ($pointer > 0 && $nstring[$pointer - 1] != 1)) { switch ($nstring[$pointer]) { case 1: $suffix = "st"; break; case 2: $suffix = "nd"; break; case 3: $suffix = "rd"; break; } } return $number . $suffix; } Text Truncate Have you noticed how the results provided by the Google search engine always neatly display snippets of information from each web site, without truncating text mid-word? Now you can cut long strings short in a similar manner using this plug-in, as shown by the screen shot in Figure 3-7, which illustrates three snippets from the first paragraph of Charles Dickens’ A Tale of Two Cities. 7 50 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 50 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 text to truncate, the maximum number of characters to allow in the new string, and a symbol or string to follow the truncated text, to show what has been done. It takes these arguments: • $text A string variable containing the text to be modified • $max A numeric variable representing the maximum number of characters allowed • $symbol A string variable to follow the new text Variables, Arrays, and Functions $temp Temporary copy of the string variable $text after initial truncating $last Numeric variable pointing to the final space character in $temp How It Works The truncation process has several parts. The first is a hard truncation down to the maximum size allowed by $max. This is done using the substr() function. Next the strrpos() function is used to find the final space in the newly truncated string. Once determined, the new string is again truncated at this new position. In the case of the Google search engine, this would be most of the process but I decided it’s unsightly to leave punctuation or another non-word character as the final character in the new string, so preg_replace() is called up to remove any non-word character that may be there. Only then is the new string returned, with the value of $symbol attached to it’s end. FIGURE 3-7 Using this plug-in, it’s easy to truncate text automatically at a word break. . s About the Plug- in This plug- in takes a string variable containing the text to process and an array containing words to be highlighted, as well as a parameter defining the type of highlighting. These. i o n s About the Plug- in This plug- in takes a string variable containing text to truncate, the maximum number of characters to allow in the new string, and a symbol or string to follow the. Functions $match String containing the current word being matched How It Works The plug- in starts iterating through the $matches array of supplied words one at a time, using a switch statement

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