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

Plug in PHP 100 POWER SOLUTIONS- P19 ppt

5 199 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 336,14 KB

Nội dung

56 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 56 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 accented text and returns a non-accented version. It requires this argument: $text A string variable containing the text to be modified Variables, Arrays, and Functions $from Array containing a list of accented characters $to Array containing non-accented versions of $from How It Works This plug-in uses the str_replace() function to replace the characters in the string $text that match those in the array $from with their non-accented counterparts in the array $to. In PHP, you can use str_replace() either to substitute single items or, as here, complete arrays of strings, with each array having 55 characters. If, for example, the character at location 23 is matched in the array $from, then the character at location 23 in the array $to is substituted. The substituted text is then returned. How to Use It To transform accented text to non-accented text, call up the plug-in as follows: echo PIPHP_RemoveAccents($text); The Plug-in function PIPHP_RemoveAccents($text) { $from = array("ỗ", "ổ", "", "ỏ", "ộ", "ớ", "ú", "ỳ", "", "ố", "ỡ", "ũ", "ự", "ọ", "ở", "ù", "ử", "ỹ", "", "õ", "ờ", "ợ", "ụ", "ỷ", "ồ", "e", "i", "ứ", "u", "ầ", "ặ", "", "", "ẫ", "", "ể", "", "", "ẩ", "è", "ề", "", "", "ậ", "ẽ", "ệ", "ĩ", "", "", "ấ", "ẻ", "ễ", "", "", "ỉ"); $to = array("c", "ae", "oe", "a", "e", "i", "o", "u", "a", "e", "i", "o", "u", "a", "e", "i", "o", "u", "y", "a", "e", "i", "o", "u", "a", "e", "i", "o", "u", "C", "AE", "OE", "A", "E", "I", "O", "U", "A", "E", "I", "O", "U", "A", "E", "I", "O", "U", "Y", "A", "E", "I", "O", "U", "A", "O"); return str_replace($from, $to, $text); } C h a p t e r 3 : Te x t P r o c e s s i n g 57 C h a p t e r 3 : Te x t P r o c e s s i n g 57 Shorten Text Sometimes, when you want to display the URL on a web page, it can be so long it looks untidy and messes up your layout. Of course, you can come up with suitable text for a hyperlink instead of showing the URL, but what about when a user posts a web address to your web site? This plug-in has a simple solution because it shortens any long URLs (or other strings) by removing the middle and only keeping the two ends. Figure 3-10 shows a long URL text string followed by a version shortened by this plug-in. You should note that when used on URLs, the shortened text is only for the displayed part of an HTML link and not the actual link itself, which must remain unchanged; the plug-in’s main use is for reducing the space that the text of a link takes up on a web page. About the Plug-in This plug-in takes a string variable containing a long URL (or other string) and returns a shortened version. It takes these arguments: • $text A string variable containing the text to be modified • $size A numeric variable containing the new string size • $mark A string variable containing a character sequence to mark the part that was removed Variables, Arrays, and Functions $len Numeric variable containing the length of the original string $a String variable containing the left-hand part of the new string $b String variable containing the right-hand part of the new string How It Works This plug-in first notes the length of the original string, and if the new required length is not smaller, simply returns the original string since there’s no shortening to do. FIGURE 3-10 Shortening URLs or other strings is easily done with this plug-in. 10 58 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 Otherwise, the left portion of the new string is created by copying half the number of characters that are to be in the new string from the left of the original string using the substr() function. The result is then stored in $a. The right-hand portion is similarly derived by taking half the number of characters required for the new string from the right of the original string. This is not quite true. Actually the left and right halves are each one character less than half the required size of the new string to allow for inserting the $mark string to signify the part of the string that has been removed. The three parts—$a, $mark, and $b—are then assembled and returned. How to Use It To shorten a URL (or other string), call the plug-in like this, where $text is the string to shorten, 60 is the new maximum size, and /-/-/ is the marker to signify the portion of the string that was removed: echo PIPHP_ShortenText($text, 60, "/-/-/"); The new shorter string will be displayed. You can replace the marker shown with any string of your choosing. The Plug-in function PIPHP_ShortenText($text, $size, $mark) { $len = strlen($text); if ($size >= $len) return $text; $a = substr($text, 0, $size / 2 -1); $b = substr($text, $len - $size / 2 + 1, $size/ 2 -1); return $a . $mark . $b; } CHAPTER 4 Image Handling 60 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 60 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 H TML and CSS have developed to such an extent that the depth and variety of features available to a web developer have never been greater. But when it comes to images and manipulating them there’s not a lot you can do, other than resize them in-browser (not a true resize, more of a squash or a stretch) and add borders. True, using JavaScript you can overlay one image on another and blend them by making one image semi-transparent, but that’s about the extent of it. That’s where PHP comes to the rescue, thanks to the GD library of image functions, which most implementations of PHP now include by default. For example, the recommended Zend Server CE from Chapter 1 already has GD enabled, as does the Mac MAMP setup. Installing the GD Library If you are running a different PHP installation and these plug-ins won’t work because GD isn’t available, then you need to add it. On Ubuntu and Debian Linux, you can install GD from the terminal with the following commands: sudo -i apt-get install php5-gd /etc/init.d/apache2 restart And on Fedora Linux you enter (as root): yum install php-gd On other setups, you’ll need to read the PHP documentation for your operating system and usually recompile PHP with GD using the with-gd option. For further details on the GD library, including installation and usage, please visit http:// php.net/manual/en/book.image.php. Otherwise, let’s get onto the next batch of ten plug-ins. Upload File A major service offered by many web sites is the facility for users to upload files and images. For example, you may wish to let your users create avatars or upload photos they have taken. Or perhaps you need to support the uploading of Word, Excel, or other types of files. Using this plug-in you can enable this feature, while retaining the security of your web site. Figure 4-1 shows the result of uploading an image file called test.jpg. FIGURE 4-1 The Upload File plug-in is easy to use and provides lots of information and error checking. 1 1 . of the original string $a String variable containing the left-hand part of the new string $b String variable containing the right-hand part of the new string How It Works This plug- in first notes. HTML link and not the actual link itself, which must remain unchanged; the plug- in s main use is for reducing the space that the text of a link takes up on a web page. About the Plug- in This plug- in. s About the Plug- in This plug- in takes a string variable containing accented text and returns a non-accented version. It requires this argument: $text A string variable containing the text

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

TỪ KHÓA LIÊN QUAN