How can i replace :), :-),O.o or B) in my a file, that is used to display chat content, by an image. Here is my code:



    //Simple exit message
    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
    fwrite($fp, "<!-- ip address=". $ip ." -->");


    header("Location: boxhead2p.php"); //Redirect the user

function loginForm(){
    <div id="loginform">
    <form action="boxhead2p.php" method="post">
        <center><p>Please enter a nickname:</p>
        <label for="name"></label>
        <input type="text" name="name" id="name" />
        <input type="submit" name="enter" id="enter" value="Enter" />
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
        echo '';



<div id="wrapper2">
    <div id="menu">
        <p class="welcome">Welcome, <b><?php echo $_SESSION['name']; ?></b></p>
        <p class="logout"><a id="exit" href="#">Exit Chat</a></p>
        <div style="clear:both"></div>
    <div id="chatbox"><?php
    if(file_exists("log.html") && filesize("log.html") > 0){
        $handle = fopen("log.html", "r");
        $contents = fread($handle, filesize("log.html"));

        echo $contents;


    <form name="message" action="" >

<center><input name="usermsg" type="text" id="usermsg" size="50" />
        <input name="submitmsg" type="submit"  id="submitmsg" value="Send" /><input type="submit" class="button"  name="submit" value="submit"></input>
<input type="hidden" name="act" value="post"></input></center>

<script type="text/javascript" src=""></script>
<script type="text/javascript">
// jQuery Document
    //If user submits the form
        var clientmsg = $("#usermsg").val();
            if (clientmsg)
    $.post("post.php", {text: clientmsg});          
        $("#usermsg").attr("value", "");
        return false;

    //Load the file containing the chat log
    function loadLog(){     
        var oldscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
            url: "log.html",
            cache: false,
            success: function(html){        
                $("#chatbox").html(html); //Insert chat log into the #chatbox div               
                var newscrollHeight = $("#chatbox").attr("scrollHeight") - 20;
                if(newscrollHeight > oldscrollHeight){
                    $("#chatbox").animate({ scrollTop: newscrollHeight }, 'normal'); //Autoscroll to bottom of div
    setInterval (loadLog, 2500);    //Reload file every 2.5 seconds

    //If user wants to end session
        var exit = confirm("Are you sure you want to end the session?");
        if(exit==true){window.location = 'boxhead2p.php?logout=true';}      


    $text = $_POST['text'];

    $fp = fopen("log.html", 'a');
    fwrite($fp, "<div class='msgln'>(".gmdate("g:i A").") <b>".$_SESSION['name']."</b>: ".$text."<br></div>");


Recommended Answers

All 5 Replies

For single:

$text = str_replace(':-)', '<img src="smiley.jpg" />', $text);

For multiple:

$txtSmileys = array (':-)', ':-(');
$imgSmileys = array ('<img src="smiley.jpg" />', '<img src="smiley-sad.jpg" />');
$text = str_replace($txtSmileys, $imgSmileys, $text);
Member Avatar

Yep, like pritaeas. With added suggestion that have a reverse transform for saving to DB. SO you save the ;-) instead of the <img>. It just means that if you want to edit the post later on, you're not wading through loads of tags and also if you decide to change the image for a particular smiley, you're not replacing all the way through your database.

how about if i want to delete the content of the log.html?

You go to the log and you remove the comment that you don't want!

Thanks alot!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.