Page 1 of 1

[SOLVED] Cannot ereg_replace smilies symbols in a textarea s

Posted: Sun Jun 20, 2004 5:58 pm
by dimitris

Code: Select all

<?php
function mass_replace($comments){
$comments=ereg_replace(":x2","<img src="comments/smilies/redface.gif" border="0">", $comments);
$comments=ereg_replace(":D2:","<img src="comments/smilies/biggrin.gif" border="0">", $comments);

$comments=ereg_replace(":sad2:","<img src="comments/smilies/sad.gif" border="0">", $comments);

$comments=ereg_replace(":smile2:","<img src="comments/smilies/smile.gif" border="0">", $comments);
$comments=ereg_replace(":D:","<img src="comments/smilies/icon_biggrin.gif" border="0">", $comments);
$comments=ereg_replace(":smile:","<img src="comments/smilies/icon_smile.gif" border="0">", $comments);

$comments=ereg_replace(":sad:","<img src="comments/smilies/icon_sad.gif" border="0">", $comments);
$comments=ereg_replace(":o","<img src="comments/smilies/icon_surprised.gif" border="0">", $comments);
$comments=ereg_replace(":lol:","<img src="comments/smilies/icon_lol.gif" border="0">", $comments);
$comments=ereg_replace(":cry:","<img src="comments/smilies/icon_cry.gif" border="0">", $comments);
$comments=ereg_replace(":evil","<img src="comments/smilies/icon_evil.gif" border="0">", $comments);
$comments=ereg_replace(":twisted:","<img src="comments/smilies/icon_twisted.gif" border="0">", $comments);
$comments=ereg_replace(":roll:","<img src="comments/smilies/icon_rolleyes.gif" border="0">", $comments);
$comments=ereg_replace(":wink:","<img src="comments/smilies/icon_wink.gif" border="0">", $comments);
$comments=ereg_replace(":paranoid:","<img src="comments/smilies/paranoid.gif" border="0">", $comments);
$comments=ereg_replace(":ponder:","<img src="comments/smilies/ponder.gif" border="0">", $comments);
$comments=ereg_replace(":bandit:","<img src="comments/smilies/bandit.gif" border="0">", $comments);
$comments=ereg_replace(":devious:","<img src="comments/smilies/devious.gif" border="0">", $comments);
$comments=ereg_replace(":disappoin","<img src="comments/smilies/disappointed.gif" border="0">", $comments);
$comments=ereg_replace(":cool:","<img src="comments/smilies/cool.gif" border="0">", $comments);
$comments=ereg_replace(":S","<img src="comments/smilies/confused.gif" border="0">", $comments);
$comments=ereg_replace(":alien:","<img src="comments/smilies/alien.gif" border="0">", $comments);
$comments=ereg_replace(";)","<img src="comments/smilies/wink.gif" border="0">", $comments);
$comments=ereg_replace(":P","<img src="comments/smilies/tongue.gif" border="0">", $comments);
$comments=ereg_replace(":right:","<img src="comments/smilies/tired.gif" border="0">", $comments);
$comments=ereg_replace(":sleeping:","<img src="comments/smilies/sleeping.gif" border="0">", $comments);
$comments=ereg_replace(":O","<img src="comments/smilies/shocked.gif" border="0">", $comments);
$comments=ereg_replace("8O","<img src="comments/smilies/icon_eek.gif" border="0">", $comments);
$comments=ereg_replace(":?","<img src="comments/smilies/icon_confused.gif" border="0">", $comments);
$comments=ereg_replace("8)","<img src="comments/smilies/icon_cool.gif" border="0">", $comments);
$comments=ereg_replace(":x","<img src="comments/smilies/icon_mad.gif" border="0">", $comments);
$comments=ereg_replace(":P","<img src="comments/smilies/icon_razz.gif" border="0">", $comments);
$comments=ereg_replace(":oops:","<img src="comments/smilies/icon_redface.gif" border="0">", $comments);
$comments=ereg_replace(":!:","<img src="comments/smilies/icon_exclaim.gif" border="0">", $comments);
$comments=ereg_replace(":?:","<img src="comments/smilies/icon_question.gif" border="0">", $comments);
$comments=ereg_replace(":idea:","<img src="comments/smilies/icon_idea.gif" border="0">", $comments);
$comments=ereg_replace(":arrow:","<img src="comments/smilies/icon_arrow.gif" border="0">", $comments);
$comments=ereg_replace(":old:","<img src="comments/smilies/old.gif" border="0">", $comments);
$comments=ereg_replace(":knocked-o","<img src="comments/smilies/knocked-out.gif" border="0">", $comments);
$comments=ereg_replace(":hurt:","<img src="comments/smilies/hurt.gif" border="0">", $comments);
$comments=ereg_replace(":cheeky:","<img src="comments/smilies/cheeky.gif" border="0">", $comments);
$comments=ereg_replace(":cross:","<img src="comments/smilies/cross.gif" border="0">", $comments);
$comments=ereg_replace(":dead:","<img src="comments/smilies/dead.gif" border="0">", $comments);
$comments=ereg_replace(":china:","<img src="comments/smilies/chinese.gif" border="0">", $comments);
$comments=ereg_replace(":ermm:","<img src="comments/smilies/ermm.gif" border="0">", $comments);
$comments=ereg_replace(":evil2:","<img src="comments/smilies/evil.gif" border="0">", $comments);
$comments=ereg_replace(":ninja:","<img src="comments/smilies/rambo.gif" border="0">", $comments);
$comments=ereg_replace(":mad:","<img src="comments/smilies/mad.gif" border="0">", $comments);
$comments=ereg_replace(":rolleyes:","<img src="comments/smilies/rolleyes.gif" border="0">", $comments);
$comments=ereg_replace(":pirate:","<img src="comments/smilies/pirate.gif" border="0">", $comments);

return $comments;
}

$comments='djfjd :wink: :D jdjfn :x2 :D2 jdsfn jds :devious: :smile2:","<img src="comments/smilies/smile.gif" border="0">", $comments);
$comments=ereg_replace(":D","<img src="comments/smilies/icon_biggrin.gif" border="0">", $comments);
$comments=ereg_replace(":smile:","<img src="comments/smilies/icon_smile.gif" border="0">", $comments);
/* fgf :paranoid: g kfgj :bandit: ';
$comments=mass_replace($comments);

echo $comments;
?>
As you can see i imitate phpbb smilies but i get this:
djfjd <img src="comments/smilies/icon_wink.gif" border="0"> D jdjfn <img src="comments/smilies/redface.gif" border="0"> D2 jdsfn jds <img src="comments/smilies/devious.gif" border="0"> <img src="comments/smilies/smile.gif" border="0">","<img src=\"comments/smilies/smile.gif\" border=\"0\">", $comments); $comments=ereg_replace("D","<img src=\"comments/smilies/icon_biggrin.gif\" border=\"0\">", $comments); $comments=ereg_replace("<img src="comments/smilies/icon_smile.gif" border="0">","<img src=\"comments/smilies/icon_smile.gif\" border=\"0\">", $comments); /* fgf <img src="comments/smilies/paranoid.gif" border="0"> g kfgj <img src="comments/smilies/bandit.gif" border="0">
between every word there is an icon_confused.gif image!

What is wrong?

I also tried to use this:

Code: Select all

<?php
$comments=ereg_replace(":)","<img src="comments/smilies/icon_smile.gif" border="0">", $comments);
?>
But i get an error that a "(" is not closed which means that it confuses the pattern with the function's syntax

Thanks in advance! This error is quite weird and i cannot find an explanation in php.net!!

Posted: Sun Jun 20, 2004 6:00 pm
by tim
you should disable smileys so people can see the actual code, you must escape special characters with a /

/:)/ is wrong, /:/)/ is right

Posted: Sun Jun 20, 2004 6:13 pm
by dimitris
tim wrote:you should disable smileys so people can see the actual code, you must escape special characters with a /

/:)/ is wrong, /:/)/ is right
ok you are right but you can guess that i use +:+) when you see a known smilie

Posted: Sun Jun 20, 2004 6:16 pm
by markl999
Might be better to use str_replace() as none of your ereg's are using regex :o

Posted: Sun Jun 20, 2004 6:17 pm
by dimitris
markl999 wrote:Might be better to use str_replace() as none of your ereg's are using regex :o
What do you mean by referring regex?
I am testing it ( str_replace() ) by the way!

Posted: Sun Jun 20, 2004 6:22 pm
by markl999
Well, ereg_ and preg_ functions invoke the regular expression engine (regex) which can be expensive. For 'simple' string replacements str_replace() is more efficient.
See http://php.net/pcre if you want to know more about regex, though it doesn't look like you need to use it at all.

Posted: Sun Jun 20, 2004 6:26 pm
by dimitris
OK!!! That worked! But why happens this mess whith ereg??? :?

Posted: Sun Jun 20, 2004 6:28 pm
by dimitris
markl999 wrote:Well, ereg_ and preg_ functions invoke the regular expression engine (regex) which can be expensive. For 'simple' string replacements str_replace() is more efficient.
See http://php.net/pcre if you want to know more about regex, though it doesn't look like you need to use it at all.
OK! I have read that ereg is more time consuming but why happened this: mixing the actual string with my code?

Thanks a lot for your help and time!

Posted: Sun Jun 20, 2004 6:28 pm
by Illusionist
Also, what i did for mine, is put all the smilies in an array, and then the corresponding images int he same order in another array, and used str_replace once like:

Code: Select all

$smi = array(':\)',':\(');
$img = array('<img src="smile.gif" border="0">','<img src="smile.gif" border="0">');
$comments = str_replace($smi,$img,$comments);

Posted: Sun Jun 20, 2004 6:30 pm
by dimitris
Illusionist wrote:Also, what i did for mine, is put all the smilies in an array, and then the corresponding images int he same order in another array, and used str_replace once like:

Code: Select all

$smi = array(':\)',':\(');
$img = array('<img src="smile.gif" border="0">','<img src="smile.gif" border="0">');
$comments = str_replace($smi,$img,$comments);
Yes that is a good idea which i used whith ereg_replace and i got the same :( problem!

Posted: Sun Jun 20, 2004 6:31 pm
by Illusionist
dimitris wrote:[OK! I have read that ereg is more time consuming but why happened this: mixing the actual string with my code?
because you ahve code in your comments, look:
dimitris wrote:

Code: Select all

$comments='djfjd   jdjfn 2 2 jdsfn jds :devious: :smile2:","<img src="comments/smilies/smile.gif" border="0">", $comments); 
$comments=ereg_replace(":D","<img src="comments/smilies/icon_biggrin.gif" border="0">", $comments); 
$comments=ereg_replace(":smile:","<img src="comments/smilies/icon_smile.gif" border="0">", $comments); 
/* fgf :paranoid: g kfgj :bandit: '; 
$comments=mass_replace($comments);

Posted: Sun Jun 20, 2004 6:32 pm
by dimitris
dimitris wrote: Yes that is a good idea which i used whith ereg_replace and i got the same :( problem!
Of course now with str_replace works fine!
Thanks a lot!

Posted: Sun Jun 20, 2004 6:33 pm
by Illusionist
it looks like it was working the way it was supossed to, only that you had actual PHP in your comment... which doesn't make sense why you can't see that