Jump to content

str_replace


golles

Recommended Posts

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:

[code]
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;
}
[/code]
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:
[code]
<?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;
}
?>
[/code]

[!--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
Link to comment
Share on other sites

You need to make your $result variable global in your function. So chnage this:[code]function replacesmiley($msg)
{[/code]
to this:
[code]function replacesmiley($msg)
{
    global $result;
[/code]
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
Link to comment
Share on other sites

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??)?
Link to comment
Share on other sites

try something like this:
[code]
<?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);
?>
[/code]

this way, you can load up your arrays from your query, but you only have to do your str_replace() once... much more efficient
Link to comment
Share on other sites

[!--quoteo(post=381071:date=Jun 7 2006, 07:12 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Jun 7 2006, 07:12 PM) [snapback]381071[/snapback][/div][div class=\'quotemain\'][!--quotec--]
try something like this:
[code]
<?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);
?>
[/code]

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
[code]
<?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>');
}
?>
[/code]

it doesn't give any errors it doesn't replace the smilies
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.