|
str_replace for smileys, and br's (HELP)
str_replace for smileys, and br's (HELP)
Currently viewing this thread: 1 (0 members and 1 guests)
 July 20th, 2004 08:30 PM
str_replace for smileys, and br's (HELP)
I have this code:
<?php
$theshout = stripslashes($r['shout']);
$theshout = str_replace('<', '<', $r['shout']);
$theshout = str_replace('>', '>', $r['shout']);
$theshout = str_replace(array_keys($smile), array_values($smile), $r['shout']);
$theshout = str_replace('
', '<br />', $r['shout']);
?>
When this displays the result, the smileys turn into text!
But the <br /> thingy is working just perfectly.
How will I solve this problem?
 July 20th, 2004 08:34 PM
what could you possibly want to know about me?
Status: Offline!
Don't you want
<?php
$theshout = stripslashes($r['shout']);
$theshout = str_replace('<', '<', $theshout);
$theshout = str_replace('>', '>', $theshout);
$theshout = str_replace(array_keys($smile), array_values($smile), $theshout);
$theshout = str_replace('', '<br />', $theshout);
?>
?
 July 20th, 2004 08:37 PM
Why?
So an empty space will become a line-break?
rofl
 July 20th, 2004 08:41 PM
what could you possibly want to know about me?
Status: Offline!
It's just that in the code you have you keep setting $theshout to be
something new, overwriting what you just did with it.
I don't know why you replace empty spaces with breaks - it's your code!
Oh! You're wanting to replace newlines with breaks... you should have "\n" in the first term then... but you still keep overwriting what you just did (which was what my suggestion should hinder - I changed the last parameter in all the str_replace-calls).
Last edited by sabrina, July 20th, 2004 08:44 PM (Edited 1 times)
 July 21st, 2004 02:44 AM
Neversidian
Status: Offline!
what do you mean the smilies are turing into text? can we see the $smile array. you need image tags around the imagepath
___________________
Neverside Development Director
PHP Snippets
BigToach.com - IT WORKS, TOACHY!
 July 21st, 2004 09:58 AM
thinking of something witty to put here
Status: Offline!
Originally posted by sabrina
Don't you want
<?php
$theshout = stripslashes($r['shout']);
$theshout = str_replace('<', '<', $theshout);
$theshout = str_replace('>', '>', $theshout);
$theshout = str_replace(array_keys($smile), array_values($smile), $theshout);
$theshout = str_replace('', '<br />', $theshout);
?>
?
Sabrina has the answer. The reason it worked for the last replace is that it kept resetting the variable until the last function. Listen to sabrina, she's a smart one 
 July 21st, 2004 04:58 PM
what could you possibly want to know about me?
Status: Offline!
Haha, thanks Radley!
Coming from you, that is a very nice compliment :}
 July 26th, 2004 12:17 AM

It doesn't work 
Ok, here's the code.
<?php
$smile = array(
'\:\)' => '<img src="emoticons/smile.gif" alt="\:\)" />',
'\:\(' => '<img src="emoticons/sad.gif" alt="\:\(" />',
'\:\D' => '<img src="emoticons/biggrin.gif" alt="\:\D" />',
'\o\_\O' => '<img src="emoticons/blink.gif" alt="\o\_\O" />',
'\:\|' => '<img src="emoticons/mellow.gif" alt="\:\|" />',
'\B\-\)' => '<img src="emoticons/cool.gif" alt="\B\-\)" />',
'\^\_\^' => '<img src="emoticons/happy.gif" alt="\^\_\^" />',
'\:\P' => '<img src="emoticons/tongue.gif" alt="\:\P" />',
'\:lol\:' => '<img src="emoticons/laugh.gif" alt="\:lol\:" />',
':\ohmy\:' => '<img src="emoticons/ohmy.gif" alt=":\ohmy\:" />',
'\;\)' => '<img src="emoticons/wink.gif" alt="\;\)" />',
':\wub\:' => '<img src="emoticons/wub.gif" alt=":\wub\:" />',
':\mad\:' => '<img src="emoticons/mad.gif" alt=":\mad\:" />',
'\-\_\-' => '<img src="emoticons/sleep.gif" alt="\-\_\-" />',
'\:dry\:' => '<img src="emoticons/dry.gif" alt="\:dry\:" />',
'\:ninja\:' => '<img src="emoticons/ninja.gif" alt="\:ninja\:" />'
);
$query = "SELECT * FROM s1 ORDER BY id DESC LIMIT 0, 10";
$result = @mysql_query($query);
while ($r = @mysql_fetch_array($result)) {
$theshout = stripslashes($r['shout']);
$theshout = str_replace('<', '<', $theshout);
$theshout = str_replace('>', '>', $theshout);
$theshout = str_replace(array_keys($smile), array_values($smile), $theshout);
$theshout = str_replace('', '<br />', $theshout);
$thename = stripslashes($r['name']);
$thename = strip_tags($r['name'], '');
$r['http'] = stripslashes($r['http']);
$r['http'] = strip_tags($r['http'], '');
$r['email'] = stripslashes($r['email']);
$r['email'] = strip_tags($r['email'], '');
if ($r['http'] == "") {
$web = "none";
} else {
$web = "<a href=\"".$r['http']."\" title=\"Visit ".$thename."'s website\">";
}
?>
EDIT: vbulletin parses the smiles so I added extra slashes
Last edited by Zeratul, July 26th, 2004 12:21 AM (Edited 1 times)
 July 26th, 2004 12:28 AM
Neversidian
Status: Offline!
you can us nl2br instead of
$theshout = str_replace('', '<br />', $theshout);
http://us2.php.net/nl2br
you might also want
http://us2.php.net/manual/en/function.htmlspecialchars.php
ya, and i dont kno why ur smilies dont work 
___________________
-Developer
-Forum Leader
-NeverNET
Last edited by aonic, July 26th, 2004 12:39 AM (Edited 1 times)
 July 26th, 2004 12:32 AM
what could you possibly want to know about me?
Status: Offline!
As I misunderstood you before (with the newlines) I think you want
<?php
$theshout = str_replace('', '<br />', $theshout);
?>
replaced with
<?php
$theshout = str_replace("\n", '<br />', $theshout);
?>
Edit:
or you could do what pureevil4 suggested.
/edit
I don't think that answers your question, though(?)
Anyway, what do you mean by doesn't work? Is it still the images?
Last edited by sabrina, July 26th, 2004 12:34 AM (Edited 1 times)
|