Jump to content


Photo

str_replace


  • Please log in to reply
5 replies to this topic

#1 golles

golles
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 07 June 2006 - 03:42 PM

I read some tutorials to get the smily thing done but I think I wasted time with it, this is the code to place the smilies:

function replacesmiley($msg)
{
$msg = str_replace(':)', '<img src="Images/smilies/1.gif" alt=":)" />', $msg);
$msg = str_replace(':-)', '<img src="Images/smilies/1.gif" alt=":-)" />', $msg);
$msg = str_replace(':(', '<img src="Images/smilies/2.gif" alt=":)"  />', $msg);
$msg = str_replace(':-(', '<img src="Images/smilies/2.gif" alt=":-)"  />', $msg);
$msg = str_replace(';)', '<img src="Images/smilies/3.gif" alt=":)" />', $msg);
$msg = str_replace(';-)', '<img src="Images/smilies/3.gif" alt=":-)" />', $msg);
$msg = str_replace(':D', '<img src="Images/smilies/4.gif" alt=":D" />', $msg);
$msg = str_replace(':-D', '<img src="Images/smilies/4.gif" alt=":-d" />', $msg);
$msg = str_replace(':d', '<img src="Images/smilies/4.gif" alt=":D" />', $msg);
$msg = str_replace(':-d', '<img src="Images/smilies/4.gif" alt=":-d" />', $msg);
$msg = str_replace(':P', '<img src="Images/smilies/5.gif" alt=":P" />', $msg);
$msg = str_replace(':-P', '<img src="Images/smilies/5.gif" alt=":-P" />', $msg);
$msg = str_replace(':p', '<img src="Images/smilies/5.gif" alt=":P" />', $msg);
$msg = str_replace(':-p', '<img src="Images/smilies/5.gif" alt=":-p" />', $msg);
$msg = str_replace(':S', '<img src="Images/smilies/6.gif" alt=":S" />', $msg);
$msg = str_replace(':-S', '<img src="Images/smilies/6.gif" alt=":-s" />', $msg);
$msg = str_replace(':s', '<img src="Images/smilies/6.gif" alt=":S" />', $msg);
$msg = str_replace(':-s', '<img src="Images/smilies/6.gif" alt=":-s" />', $msg);
$msg = str_replace(':|', '<img src="Images/smilies/7.gif" alt=":|" />', $msg);
$msg = str_replace(':-|', '<img src="Images/smilies/7.gif" alt=":-|" />', $msg);
$msg = str_replace(':*(', '<img src="Images/smilies/8.gif" alt=":*(" />', $msg);
$msg = str_replace(':O', '<img src="Images/smilies/9.gif" alt=":O" />', $msg);
$msg = str_replace(':-O', '<img src="Images/smilies/9.gif" alt=":-O" />', $msg);
$msg = str_replace(':o', '<img src="Images/smilies/9.gif" alt=":o" />', $msg);
$msg = str_replace(':-o', '<img src="Images/smilies/9.gif" alt=":-o" />', $msg);
$msg = str_replace('(A)', '<img src="Images/smilies/10.gif" alt="(A)" />', $msg);
$msg = str_replace('^O)', '<img src="Images/smilies/11.gif" alt="^O)" />', $msg);
$msg = str_replace('^o)', '<img src="Images/smilies/11.gif" alt="^o)" />', $msg);
$msg = str_replace('(H)', '<img src="Images/smilies/12.gif" alt="(H)" />', $msg);
$msg = str_replace('(FE)', '<img src="Images/smilies/13.gif" alt="(FE)" />', $msg);
$msg = str_replace(':@', '<img src="Images/smilies/14.gif" alt=":@" />', $msg);
$msg = str_replace(':-@', '<img src="Images/smilies/14.gif" alt=":-@" />', $msg);
$msg = str_replace('(6)', '<img src="Images/smilies/15.gif" alt="(6)" />', $msg);
$msg = str_replace(':$', '<img src="Images/smilies/16.gif" alt=":$" />', $msg);
$msg = str_replace(':-$', '<img src="Images/smilies/16.gif" alt=":-$" />', $msg);
$msg = str_replace('+o(', '<img src="Images/smilies/17.gif" alt="+o(" />', $msg);
$msg = str_replace('(L)', '<img src="Images/smilies/18.gif" alt="(L)" />', $msg);
$msg = str_replace('(U)', '<img src="Images/smilies/19.gif" alt="(U)" />', $msg);
$msg = str_replace('(K)', '<img src="Images/smilies/20.gif" alt="(K)" />', $msg);
$msg = str_replace('(B)', '<img src="Images/smilies/21.gif" alt="(B)" />', $msg);
$msg = str_replace('({)', '<img src="Images/smilies/22.gif" alt="({)" />', $msg);
$msg = str_replace('(})', '<img src="Images/smilies/23.gif" alt="(})" />', $msg);
$msg = str_replace('(Z)', '<img src="Images/smilies/24.gif" alt="(Z)" />', $msg);
$msg = str_replace('(X)', '<img src="Images/smilies/25.gif" alt="(X)" />', $msg);
$msg = str_replace('(8)', '<img src="Images/smilies/26.gif" alt="(8)" />', $msg);
$msg = str_replace('(I)', '<img src="Images/smilies/27.gif" alt="(I)" />', $msg);
$msg = str_replace('(Y)', '<img src="Images/smilies/28.gif" alt="(Y)" />', $msg);
$msg = str_replace('(N)', '<img src="Images/smilies/29.gif" alt="(N)" />', $msg);
$msg = str_replace('(FU)', '<img src="Images/smilies/30.gif" alt="(FU)" />', $msg);
return $msg;
}
I thought that this could be easier with a database,,, I tryed to generate the above with a mysql_fetch_row but it didn't worked :S

anyone an idea how to do it? I got the following in the DB: id, Code (ie :)) and URL (ie Images/smilies/1.gif).

hmm before I forget this it what I used:
<?php
$con = mysql_connect("localhost","root");
mysql_select_db("golles", $con);

$result = mysql_query("SELECT * FROM smilies ORDER BY id", $con);

function replacesmiley($msg)
{
while (list($id, $URL, $Code) =
mysql_fetch_row($result)){
$msg = str_replace($Code, '<img src="' . $URL . '" alt="'. $Code . '" />', $msg);
}
return $msg;
}
?>

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\golles\Mijn documenten\Website\golles\gastenboek.php on line 44[/quote]

I know I made a damn long message, but I think everythig is needed,,,
anway, all help will be appriciated!
(btw posting something to do this on a beter way is also good!


//golles

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 June 2006 - 03:51 PM

You need to make your $result variable global in your function. So chnage this:
function replacesmiley($msg)
{
to this:
function replacesmiley($msg)
{
    global $result;
Variables dont have global scope when used in a function so you need to declare the variables that are defined out side of the function as global

#3 golles

golles
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 07 June 2006 - 03:57 PM

that won't help, now it will only give me the last message from the guestbook, and it doesn't replace the smilies with images, do you know a beter way to get smilies on the guestbook page? (with the SQL database if posible??)?

#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 07 June 2006 - 04:03 PM

Ok. What is you current setup. How are you getting your guestbook records out of the database? Could you post the code here.

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 07 June 2006 - 05:12 PM

try something like this:
<?php
$sql = mysql_query("SELECT * FROM smilies ORDER BY id");
$smilies = array();
$replace = array();
while ($x = mysql_fetch_array($sql)) {
  $smilies[] = $x['Code'];
  $replace[] = "<img src=\"$x[URL]\" />";
}

$String = str_replace($smilies, $replace, $String);
?>

this way, you can load up your arrays from your query, but you only have to do your str_replace() once... much more efficient
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 golles

golles
  • Members
  • PipPipPip
  • Advanced Member
  • 34 posts

Posted 07 June 2006 - 07:28 PM

[!--quoteo(post=381071:date=Jun 7 2006, 07:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 7 2006, 07:12 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
try something like this:
<?php
$sql = mysql_query("SELECT * FROM smilies ORDER BY id");
$smilies = array();
$replace = array();
while ($x = mysql_fetch_array($sql)) {
  $smilies[] = $x['Code'];
  $replace[] = "<img src=\"$x[URL]\" />";
}

$String = str_replace($smilies, $replace, $String);
?>

this way, you can load up your arrays from your query, but you only have to do your str_replace() once... much more efficient
[/quote]
ok, thank you, I was testing but I'm assuming I do something wrong,,,
here is the php part,,, please tell me what I'm doing wrong
<?php
function replacesmiley($String)
{

$con = mysql_connect("localhost","root", "");
mysql_select_db("golles", $con);

$sql = mysql_query("SELECT * FROM smilies ORDER BY id");
$smilies = array();
$replace = array();
while ($x = mysql_fetch_array($sql)) {
  $smilies[] = $x['Code'];
  $replace[] = "<img src=\"$x[URL]\" />";
}

$String = str_replace($smilies, $replace, $String);
return $String;
}

$con = mysql_connect("localhost","root", "");
mysql_select_db("golles", $con);

$result = mysql_query("SELECT * FROM gastenboek ORDER BY id DESC", $con);

while (list($id, $naam, $mail, $site, $datum, $ip, $bericht) =
mysql_fetch_row($result)){

$naam    = htmlspecialchars($naam);
$mail    = htmlspecialchars($mail);
$site = htmlspecialchars($site);
$bericht = htmlspecialchars($bericht);
$bericht = replacesmiley($bericht);
$bericht = nl2br($bericht);

if ($site == 'http://'){ $site = ''; } 
if ($mail == ''){$naammail = $naam;} else {$naammail = '<a href="mailto:' . $mail . '" class="GB">' . $naam . '</a>'; }

echo ('<table class="gastenboek" align="center">
<tr>
<td class="schrijfkop" width="40%">Naam: ' . $naammail . '</td>
<td class="schrijfkop" width="45%"><a href="' . $site . '" target="_blank" class="GB">' . $site . '</a></td>
<td class="schrijfkop">' . $datum . '</td>
</tr>
<tr>
<td colspan="3">' . $bericht . '</td>
</tr>
</table><br><br>');
}
?>

it doesn't give any errors it doesn't replace the smilies




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users