[SOLVED] Cannot ereg_replace smilies symbols in a textarea s

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

[SOLVED] Cannot ereg_replace smilies symbols in a textarea s

Post 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!!
Last edited by dimitris on Sun Jun 20, 2004 6:13 pm, edited 1 time in total.
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

you should disable smileys so people can see the actual code, you must escape special characters with a /

/:)/ is wrong, /:/)/ is right
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

Might be better to use str_replace() as none of your ereg's are using regex :o
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post 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!
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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.
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post by dimitris »

OK!!! That worked! But why happens this mess whith ereg??? :?
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post 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!
Last edited by dimitris on Sun Jun 20, 2004 6:28 pm, edited 1 time in total.
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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);
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post 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!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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);
User avatar
dimitris
Forum Contributor
Posts: 110
Joined: Wed Jan 14, 2004 3:47 am
Location: Athens, Greece

Post 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!
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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
Post Reply