Jump to content

[SOLVED] PHP Script to replace :) with <img src="images/smilies/smile.gif"></a>


Robert Elsdon

Recommended Posts

Now, heres my index page (home page of site).

 

<?PHP session_start();?>
<? include("includes/connect.php");
?>






<html>
<head>
<title> Local Host </title>
<link href="includes/style.css" rel="stylesheet" type="text/css">
</head>
<div align="center">
<body>

<div class="nc_wrapper">

<? include ('includes/header.php'); ?>

<div class="spacer"></div>

<? include ('includes/left-menu.php'); ?>


<div class="content">
<table cellspacing="1" cellpadding="0" border="0" style="width:100%; background:#000000">
<tr><td class="content_head">
Content
</td></tr>

<tr><td class="actual_content">



Welcome to localhost, This is just a temperary website as for now untill i get it 100% complete! THEN i am giving it to a good friend of mine who owns - <a href="http://pulse-box.com" target="new_window">Pulse-Box</a> so he can sell it to any of yous who like it.

 :)  :) 

</td></tr>

<tr><td class="content_foot"></td></tr>
</table>
</div>
</div>


<? include ('includes/footer.php') ?>
</div>
</body>
</html>

 

How would i make it change the :) in the content with a image by using this script?

 

<?php

$string = "This is a test ";
echo str_replace(" ", " <img src='images/smilies/smile.gif'/>", $string);

?>

 

Regards

 

Robert

 

Link to comment
Share on other sites

You can't do it like that.  Once the content is output, you can't go back and change it.  At least, not with php.  I suppose you could use some client-side scripting to do it, but I don't think that's what you're really looking to do. 

 

I have to ask...if you have a page hardcoded like that, why not just put the image tags in there in the first place?

Link to comment
Share on other sites

How would i do multiple tags ? heres an example but it doesnt work? You might get what i mean..

 

 

<?php

$string = "This is a test ";
echo str_replace(" ", " <img src='images/smilies/smile.gif'/>", $string);

echo str_replace(" ", " <img src='images/smilies/sad.gif'/>", $string);

echo str_replace(" ", " <img src='images/smilies/grin.gif'/>", $string);

echo str_replace(" ", " <img src='images/smilies/tounge.gif'/>", $string);

?>

 

 

The last 3 echo str_replace are examples ;)

Link to comment
Share on other sites

The form to post messages onto the shoutbox

 

<form action="<? echo $php_self ?>#bottom" method="post">
<center>




<?
mysql_connect("localhost","root","12345678");
mysql_select_db("shoutbox");

if($_POST['submit']) { ?>

<?
$name	= $_POST['name'];
$message= $_POST['message'];
$time=date("h:ia d/m/y");

if(!$message) { ?>
Please enter a message.
<? } else if($message == 'Enter Message') { ?>
<font color="#b2eb62">
PLEASE ENTER A MESSAGE</font>
<? } else { ?>
<?
$insertshout="INSERT INTO shoutbox(name,message,time) VALUES ('$name', '$message','$time')";
mysql_query($insertshout) or die("Cannot insert shout");
?>
<script type="text/JavaScript">window.location='index.php';</script>
<? } ?>
<? } else { } ?>





<table>

<? if($_SESSION['logged_in'] == 'Yes') { ?>

<tr><td>
<center>Username :</center> <center><input class="text_input" type='text' value='<?=(ucwords($_SESSION[user_name]));?>' name='name' size=30 maxlength='100' readonly="yes"></center>


</td><tr>

<? } else { ?>


<? } ?>

<tr><td>
<center>Message :</center> <center><input class="text_input" type="text" maxlength="300" name="message" id="message" value="Enter Message" onclick=document.getElementById('message').value=""; onBlur="shoutmessage();"></center>
</td></tr>

<tr><td>
<center><input class="button" type="submit" name="submit" value="Post Shout" style="margin:0px;"></center>
</td></tr>
</table>

</form>

 

And heres the shoutbox itself which displays the posts

 

<?PHP session_start(); ?>




<?
include("includes/connect.php");
// Get the messages from the databse
$get_messages = mysql_query("select * from shoutbox");
$amountofmessages = mysql_num_rows($get_messages);

$select_shouts = mysql_query("select * from shoutbox ORDER BY id DESC LIMIT 10");
?>	

<table cellpadding="0" cellspacing="0" border="0" style="width: 100%;">

<? 
$color='dark';
while($r=mysql_fetch_array($select_shouts)) { 
if($color == 'dark') { ?>

<tr class="dark">
<td><?=$r["time"];?></td>
<td><?=$r["date"];?></td>
<td><b><font color="#b2eb62"><?=$r["id"];?></font></b>
</td>
</tr>

<tr>
<td colspan="3" align="left" style="padding:3px;">
<b><?=$r["name"];?></b><b><font color="#b2eb62"> -</b> </font><?=$r['message'];?>
</td>
</tr>

<? $color='light';} else { ?>

<tr class="dark">

<td><?=$r["time"];?></td>
<td><?=$r["date"];?></td>
<td><b><font color="#b2eb62"><?=$r["id"];?></font></b>
</td>
</tr>

<tr>
<td colspan="3" align="left" style="padding:3px;">
<b><?=$r["name"];?></b><b><font color="#b2eb62"> -</b> </font><?=$r['message'];?>
</td>
</tr>

<? $color='dark';} } ?>

</table>


<? if(!$_SESSION['logged_in'] == 'Yes') { ?>
<p align="center">
<font color="#b2eb62">you need to be logged in to view this form to post on shoutbox! </ br>
Please, <a href="index.php">Login</a> <b>/</b> <a href="register.php">Register</a> to post a message!
</p></font>
<? } ?>

 

Regards, Robert

 

Link to comment
Share on other sites

Okay, in the shoutbox code that displays the actual shouts, you have these 2 lines:

 

while($r=mysql_fetch_array($select_shouts)) {
if($color == 'dark') { ?>

 

change that to

 

while($r=mysql_fetch_array($select_shouts)) {
$r['message'] = str_replace("", "<img src='images/smilies/smile.gif'/>", $r['message']);
if($color == 'dark') { ?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.