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

Plug in PHP 100 POWER SOLUTIONS- P48 docx

5 190 0
Tài liệu đã được kiểm tra trùng lặp

Đ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 251,97 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

About the Plug-in This plug-in accepts a string of text to search for emoticons to replace with smiley GIF images.. It takes these arguments: • $text The text to process for emoticons •

Trang 1

About the Plug-in This plug-in accepts a string of text to search for emoticons to replace with smiley GIF images It takes these arguments:

• $text The text to process for emoticons

• $folder The folder in which you have saved the smiley GIFs

Variables, Arrays, and Functions

$chars Array of emoticons to search for

$gifs Array of GIF filenames without the gif extensions

$j Integer index for iterating through the arrays How It Works

This plug-in supports 20 different types of emoticons and their associated GIF smileys The

emoticons are stored in the array $chars, and the GIF filenames (minus the gif extensions)

are housed in the array $gifs They are all in groups to make the plug-in easy to modify

For example, the first four elements of each array are the angry emoticons and smileys.

The emoticons need to be replaced with the correct HTML with which to display an associated GIF image, so the argument $folder is used to provide the correct path to the images To ensure that paths are accepted either with or without trailing slashes, the plug-in first removes any such slashes, adding it back later where required

Then a for loop is entered to iterate through all of the names in $gifs, replacing each

element with the HTML that will reference each GIF, and providing its width, height, alt, and

title attributes

With the $gifs array now suitably processed, both arrays are passed to the str_

ireplace() function, which then replaces all occurrences of any emoticons in the $chars array with the replacement code in the $gifs array It does this while ignoring the case of each character so that, for example, both :S and :s will be replaced with the HTML for

displaying the puzzled.gif smiley.

How to Use It Before you call this plug-in, you must download the folder of GIFs from the companion

web site at www.pluginphp.com When you click the Download link, all the plug-ins will be

F IGURE 8-9 Use this plug-in to replace text emoticons with smiley GIF images.

Trang 2

downloaded in a Zip file called plug-ins.zip, which you’ll need to extract Once extracted, you’ll immediately see the GIFs in the folder smileys To use them, copy the smileys into a folder

within your document root on your web server so that they are accessible by a web browser

If you wish, you can replace some or all of the icons, as long as you keep the same

filenames for the same smiley type But remember, you may need to alter the width and height

attributes in the code if your new smileys have different dimensions (or just leave those attributes out if the dimensions vary) The provided set of GIFs are all 15 × 15 pixels in size Also, make sure you don’t rename or delete any of the files, otherwise the plug-in will not work correctly because it assumes all the files have the names specified in the array $gifs You use the plug-in by passing it the text to process and the path to the folder of GIFs,

like this (where smileys is the name of the folder):

echo PIPHP_ReplaceSmileys($text, 'smileys');

To test it, make sure $text contains a few emoticons, like ‘:) :] :D XD’, and so on

If you prefer, you can also assign the result of calling the plug-in to a string variable, where it can then be used elsewhere in your program, like this:

$html = PIPHP_ReplaceSmileys($text, 'smileys');

The Plug-in

function PIPHP_ReplaceSmileys($text, $folder) {

$chars = array('>:-(', '>:(', 'X-(', 'X(', ':-)*', ':)*', ':-*', ':*', '=*', ':)', ':]',

':-)', ':-]', ':(', ':C', ':[', ':-(', ':\'(', ':_(', ':O', ':-O',

':P', ':b', ':-P', ':-b', ':D', 'XD',

';)', ';-)', ':/', ':\\', ':-/', ':-\\', ':|',

'B-)', 'B)', 'I-)', 'I)', ':->', ':>', ':X', ':-X', '8)', '8-)', '=-O', '=O', 'O.o', ':S', ':-S', '*-*', '*_*');

$gifs = array( 'angry', 'angry', 'angry', 'angry', 'kiss', 'kiss', 'kiss', 'kiss', 'kiss', 'smiley', 'smiley',

'happy', 'happy', 'sad', 'sad', 'sad', 'cry', 'cry', 'cry', 'shocked', 'shocked',

'tongue', 'tongue', 'tongue', 'tongue', 'laugh', 'laugh',

Trang 3

'wink', 'wink', 'uneasy', 'uneasy', 'uneasy', 'uneasy', 'blank',

'cool', 'cool', 'sleep', 'sleep', 'sneaky', 'sneaky', 'blush', 'blush', 'wideeye', 'wideeye', 'uhoh', 'uhoh', 'puzzled', 'puzzled', 'puzzled', 'dizzy', 'dizzy');

if (substr($folder, -1) == '/') $folder = substr($folder, 0, -1);

for ($j = 0 ; $j < count($gifs) ; ++$j) $gifs[$j] = "<image src='$folder/$gifs[$j].gif' " "width='15' height='15' border='0' alt='$gifs[$j]' " "title='$gifs[$j]' />";

return str_ireplace($chars, $gifs, $text);

}

Replace SMS Talk

Sometimes your users will use text speak in their posts, so-called because it evolved through

the use of texting messages on mobile phones It’s a more compact and less time consuming way of communicating that’s also often used on Twitter due to its similar restriction on message length

But one thing it isn’t is pretty So if you would like to clean up posts a little before adding them to your site, this plug-in will do the trick Figure 8-10 shows the result of passing a string containing several text speak acronyms to the plug-in, which it suitably corrects

About the Plug-in This plug-in accepts a string which, if it contains recognized text speak acronyms, is converted

to standard English and returned It takes this argument:

• $text The text to be processed

F IGURE 8-10 Want to translate your users’ text speak? Do it with this plug-in.

60

Trang 4

Variables, Arrays, and Functions

$sms Array of text speak acronyms to be replaced and their equivalents in standard English

$from1 Array based on the $sms array with regular expression operators for processing

uppercase

$to1 Array containing the standard English replacements for $from1

$from2 Array based on the $sms array with regular expression operators for processing

lowercase

$to2 Array containing the standard English replacements for $from2

$j Integer index for iterating through the arrays How It Works

This function is based around the array $sms, which contains pairs of data: a text speak SMS acronym and its equivalent in standard English I built the plug-in this way to make it very easy for you to change any pairs or add more of your own Just remember to precede any apostrophes with a \ escape character (like this \') to avoid getting an error message Because of the way these pairs are stored, they do need a little massaging to get them into a state with which $text can be processed This is done by first initializing two pairs of

from and to arrays: $from1 / $to1 and $from2 / $to2 Then a for loop iterates through the

$sms array, extracting the from and to halves of each pair

This is done twice so all the uppercase text speak acronyms can have an effect different from the lowercase ones For example, the acronym BTW should be replaced with By the way (note the initial capital letter), but the acronym btw should be replaced with by the way (with no initial capital letter)

To achieve this, the $from1 array is populated with the correct regular expression to match only the uppercase acronyms It does this using the \b operator, which marks the boundary between a word and a non-word character, so that only acronyms are matched and not groups of the same letters that may occur within words

Next, $to1 has each replacement string passed through the ucfirst() function, which forces the initial letter of a string to uppercase, before being assigned the resulting value

On the other hand, $from2 uses the same regular expression as $from1, except that a letter i is added after the closing / of the expression This tells the preg_replace() function that matching should take place regardless of the whether acronyms are upper- or lowercase (or even a combination) The $to2 array doesn’t get passed through the ucfirst() function,

so those replacements remain unchanged

When the loop has completed, the arrays will all be correctly populated, and the preg_ replace() function will have been called twice to perform the replacements The first time,

$from1 acronyms are replaced with $to1 standard English and have their first letters capitalized The second time, $from2 acronyms are replaced with $to2 standard English equivalents The result of all these translations is then returned

How to Use It

To use this plug-in, pass a string of text that you think may contain text speak to it, like this:

$text = "FYI, afaik imho this is a cool plug-in LOL.";

echo PIPHP_ReplaceSMSTalk($text);

Trang 5

The plug-in will then make any required substitutions and return the result, which in the preceding case would be: “For your information, as far as I know in my humble opinion this is a cool plug-in Laughing out loud”

The Plug-in

function PIPHP_ReplaceSMSTalk($text) {

$sms = array('ABT2', 'about to', 'AFAIC', 'as far as I\'m concerned', 'AFAIK', 'as far as I know',

'AML', 'all my love', 'ATST', 'at the same time', 'AWOL', 'absent without leave', 'AYK', 'as you know',

'AYTMTB', 'and you\'re telling me this because?', 'B4', 'before',

'B4N', 'bye for now', 'BBT', 'be back tomorrow', 'BRB', 'be right back', 'BTW', 'by the way', 'BW', 'best wishes', 'BYKT', 'but you knew that', 'CID', 'consider it done', 'CSL', 'can\'t stop laughing', 'CYL', 'see you later',

'CYT', 'see you tomorrow', 'DGA ', 'don\'t go anywhere', 'DIKU', 'do I know you?', 'DLTM', 'don\'t lie to me', 'FF', 'friends forever', 'FYI', 'for your information', 'GBH', 'great big hug',

'GG', 'good game', 'GL', 'good luck', 'GR8', 'great', 'GTG', 'got to go', 'HAK', 'hugs and kisses', 'ILU', 'I love you', 'IM', 'instant message', 'IMHO', 'in my humble opinion', 'IMO', 'in my opinion',

'IMS', 'I\'m sorry', 'IOH', 'I\'m outta here', 'JK', 'just kidding', 'KISS', 'Keep it simple silly', 'L8R', 'later',

'LOL', 'laughing out loud', 'M8 ', 'mate',

'MSG', 'message', 'N1', 'nice one', 'NE1', 'anyone?', 'NMP', 'not my problem', 'NOYB', 'none of your business',

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