Each expression in each comment added to the database is expressed in a special character, such as "[Face1]" represents a smiley face, "[Face2]" represents a sad expression. Just, when this comment is displayed, the special characters of all the expressions in the comments are replaced with the corresponding pictures.
The code is as follows |
Copy Code |
<?php /** * Comment Add expression--insert emoticons at cursor (www.111cn.net) */ $content = isset ($_post["content")? $_post["Content"]: '; if (!empty ($content)) { for ($i = 1; $i <= $i + +) { $content = Str_replace (' face '. $i. '] ', ' ', $content); Replace special characters with picture display (also available as regular substitutions, but are said to be inefficient) } Echo ' The content you entered is: '. $content. ' <br/> '; }
?> <script type= "Text/javascript" > Inserts a picture 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= "submitted"/> </form> |