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:

<?
session_start();

if(isset($_GET['logout'])){ 
$ip=$_SERVER['REMOTE_ADDR'];


    //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 ." -->");



    fclose($fp);

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

function loginForm(){
    echo'
    <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" />
    </form>
    </div>
    ';
}
if(isset($_POST['enter'])){
    if($_POST['name'] != ""){
        $_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
    }
    else{
        echo '';
    }

}

?>



<?php
if(!isset($_SESSION['name'])){
    loginForm();
}
else{
?>
<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>   
    <div id="chatbox"><?php
    if(file_exists("log.html") && filesize("log.html") > 0){
        $handle = fopen("log.html", "r");
        $contents = fread($handle, filesize("log.html"));
        fclose($handle);

        echo $contents;
    }

    ?></div>

    <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>

    </form>
</div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script type="text/javascript">
// jQuery Document
$(document).ready(function(){
    //If user submits the form
    $("#submitmsg").click(function(){   
        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;
        $.ajax({
            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
    $("#exit").click(function(){
        var exit = confirm("Are you sure you want to end the session?");
        if(exit==true){window.location = 'boxhead2p.php?logout=true';}      
    });
});
</script>
<?php
}
?>

And

<?
session_start();
if(isset($_SESSION['name'])){
    $text = $_POST['text'];

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

Sanchixx

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 for diafol

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, networking, learning, and sharing knowledge.