Jump to content

smileys


yoshida

Recommended Posts

Hi.How do I make an array to replace specific text with an image? I'm writing a guestbook and would like it to contain smileys.Thanks in advance.EDIT:Here's the guestbook, a onepage script.

if ($pageid == 'gastenboek') {		if ($mode == 'write') {		if (isset($_POST['akkoord'])) {				$gnam=$_POST['gnam'];			$gmal=$_POST['gmal'];			$gdat=$_POST['date'];			$gcon=$_POST['gcon'];						if ($gnam == "" || $gmal == "" || $gcon == "") {				echo "Niet alle verplichte velden zijn ingevuld. <a href=index.php?pageid=write>opnieuw</a>";			}			else {				mysql_connect('localhost',$user,$pass);				mysql_select_db($dbas) or die( "Unable to select database");								$query="INSERT INTO gastenboek VALUES ('','gnam','gmal','gdat','gcon'";				mysql_query($query);				echo "Uw bericht is toegevoegd, dank u wel.";								mysql_close();			}		}		else {			echo "<form action=index.php?pageid=gastenboek method=post>";			$date=date('ymdHi');			$maand_array = array("januari", "februari", "maart", "april","mei", "juni", "juli", "augustus", "september","oktober", "november", "december");			$datum = date("j ") . $maand_array[date("n") - 1] . date(" Y");			echo "<table><tr><td><b>Naam</b><td><b>E-mailadres</b></tr><tr><td><input type=text name=gnam size=30><td> <input type=text name=gmal size=40></tr></tr><table><br><b>Datum:</b>$datum<input type=hidden name=gdat value=$date>";			echo "<br><textarea rows=20 cols=70 name=gcon></textarea><br><input type=submit name=akkoord value='voeg toe'>";		}	}	else {		mysql_connect('localhost',$user,$pass);		mysql_select_db($dbas) or die( "kan database niet vinden.");				$query="SELECT * from GASTENBOEK ORDER BY date ASC";		$result=mysql_query($query);				$num=mysql_num_rows($result);				$i=1;		while($i < $num) {			$name=mysql_result($result,$i,"gnam");			$mail=mysql_result($result,$i,"gmal");			$date=mysql_result($result,$i,"gdat");			$gcon=mysql_result($result,$i,"naam");			$i++;			echo "";		}	}}

This is a prove of concept, as well as aditional info to tell me what to implement and how and where. You're free to use this concept.When you click the link to the guestbook, it will propagate that 'pageid=gastenboek'. It'll check if 'mode' is set to 'write' (a link on the guestbook itself) and if so, if the 'submit' button has been hit. If not, the script will generate a form and the submit button. When you hit submit the form will be posted into the database, giving it the date and timestamp (as for right now) 0606291402 (ymdHi) to sort each post by year, month, day, hour and minute.Let's assume you're reading, and never hit the 'write' link. Everything that all other folks have written will be echoed, sorted by time and date. I haven't written that function yet, as the stylesheet isn't done. I'll get to it later.Anyhow, I'm implementing this beauty in my eggshell site builder (along with several newspages and a content manager). I'll be able to show the script and a working version 'pretty soon' since it'll become the backbone for my site.

Link to comment
Share on other sites

$postcode = $_POST['postbox'];$find = array();$replacewith = array();$find[] = ":lol:";$find[] = ":)";$replacewith[] = '<img src="lol.gif" alt="LOL"/>';$replacewith[] = '<img src="smile.gif" alt="smile"/>';for($i=0;$i<sizeof($find);$i++){     $postcode = str_replace($find[$i],$replacewith[$i],$postcode);}

something like that. Please excuse any syntax errors, I didn't have time to test it :)

Link to comment
Share on other sites

Smells great. ^^Thanks. I should learn to find my own way in this... then again: what good would a forum be? :)

Link to comment
Share on other sites

Agreed, I crawl when I'm drunk, walk when I'm stupid and bike when I'm in the mood. Smart people use a car (hmm... cheatsheets?) or public transport (forum-ish).Sorry for the jabbing, my eggshell made me wake up at 4 AM for three nights in a row, and kept me pondering untill 6:30 (when I had to wake up for work). Yeah... what am I doing to myself... :)Final results by the end of next week.[/hype-ing]

Link to comment
Share on other sites

$postcode = $_POST['postbox'];$find = array();$replacewith = array();$find[] = ":lol:";$find[] = ":)";$replacewith[] = '<img src="lol.gif" alt="LOL"/>';$replacewith[] = '<img src="smile.gif" alt="smile"/>';for($i=0;$i<sizeof($find);$i++){     $postcode = str_replace($find[$i],$replacewith[$i],$postcode);}

That's good code, but I ran into a shortcut while needing to do something like this myself. str_replace can accept any parameter as an array. You can read the details on the php.net page, but you can shorten that code with this:
$postcode = $_POST['postbox'];$find = array();$replacewith = array();$find[] = ":lol:";$find[] = ":)";$replacewith[] = '<img src="lol.gif" alt="LOL"/>';$replacewith[] = '<img src="smile.gif" alt="smile"/>';$postcode = str_replace($find,$replacewith,$postcode);

http://www.php.net/manual/en/function.str-replace.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...