This article introduces how to insert characters in comments, emotices, and images at the cursor. For more information, see references.
Each comment added to the database represents a special character. For example, "[face1]" represents a smiling face, and "[face2]" represents a sad face. However, before the comment is displayed, replace all the special characters in the comment with the corresponding image.
| The Code is as follows: |
Copy code |
|
<? Php
/**
* Add emoticons to comments-insert emoticons (www. bKjia. c0m) at the cursor)
*/
$ Content = isset ($ _ POST ["content"])? $ _ POST ["content"]: '';
If (! Empty ($ content ))
{
For ($ I = 1; $ I <= 13; $ I ++)
{
$ Content = str_replace ('[face '. $ I. ']', ' ', $ content); // Replace special characters with images for display (you can also replace regular expressions, but it is said that regular expressions are less efficient)
}
Echo 'the content you entered is: '. $ content.' <br/> ';
}
?>
<Script type = "text/javascript">
// Insert the image character JS at the cursor
Function addExpression (myField, myValue)
{
MyField = document. getElementById (myField );
// IE
If (document. selection)
{
MyField. focus ();
Sel = document. selection. createRange ();
MyValue = "[face" + myValue + "]";
Sel. text = myValue;
Sel. select ();
}
// MOZILLA
Else if (myField. selectionStart | myField. selectionStart = '0 ')
{
Var startPos = myField. selectionStart;
Var endPos = myField. selectionEnd;
// Save scrollTop before insert
Var restoreTop = myField. scrollTop;
MyValue = "[face" + myValue + "]";
MyField. value = myField. value. substring (0, startPos) + myValue + myField. value. substring (endPos, myField. value. length );
If (restoreTop> 0)
{
// Restore previous scrollTop
MyField. scrollTop = restoreTop;
}
MyField. focus ();
MyField. selectionStart = startPos + myValue. length;
MyField. selectionEnd = startPos + myValue. length;
}
Else
{
MyField. value + = myValue;
MyField. focus ();
}
}
</Script>
<Br/> <p/>
<Br/>
<Form action = "" method = "post">
<Textarea name = "content" id = "content"> </textarea>
<Input type = "submit" value = "submit"/>
</Form>
|