Monkuar Posted December 7, 2011 Share Posted December 7, 2011 This code WORKS FINE: '#(?<!\w):thumbsup:(?!\w)#i' => '<img src=html/emoticons/thumbsup.gif alt=":thumbsup\:">', so Whenever I do :thumbsup: in my post, it makes the thumbsup smiley! it works nice! but I need the following to work: '#(?<!\w)<3(?!\w)#i' => '<img src=html/emoticons/heart2.gif alt="<3">', See how I want to type "<3" to get my heart2.gif showing? For some reason it's not passing through and displays nothing, I tried to do <\3 but it still won't work, any ideas? I need to escape somethiing because of the "<" character is messing me all up. Thanks for the help Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/ Share on other sites More sharing options...
ManiacDan Posted December 7, 2011 Share Posted December 7, 2011 Works for me: php > $a = "I <3 PHP"; php > echo preg_replace('#(?<!\w)<3(?!\w)#i', '{{HEART}}', $a); I {{HEART}} PHP php > -Dan Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295374 Share on other sites More sharing options...
Monkuar Posted December 7, 2011 Author Share Posted December 7, 2011 Works for me: php > $a = "I <3 PHP"; php > echo preg_replace('#(?<!\w)<3(?!\w)#i', '{{HEART}}', $a); I {{HEART}} PHP php > -Dan Hmm Sir, let me show you my full function then maybe will help, cause I copied your "#(?<!\w)<3(?!\w)#i" and it's not working still. Here you go mate: $smilies=array ( '#(?<!\w):thumbsup:(?!\w)#i' => '<img src=html/emoticons/thumbsup.gif alt=":thumbsup\:">', '#(?<!\w)<3(?!\w)#i' => '<img src=html/emoticons/heart2.gif alt="<3">', ); $txt=preg_replace(array_keys($smilies), array_values($smilies), $txt,5); It's not replacing the <3.... really dunno what is problem, is it because of the array_keys? Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295376 Share on other sites More sharing options...
ManiacDan Posted December 7, 2011 Share Posted December 7, 2011 The 5 in your preg_replace means "only do it 5 times." Do you have 5 :thumbsup: before the heart? Your function (which is not a function) works for me when I copy/paste it. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295384 Share on other sites More sharing options...
Monkuar Posted December 7, 2011 Author Share Posted December 7, 2011 The 5 in your preg_replace means "only do it 5 times." Do you have 5 :thumbsup: before the heart? Your function (which is not a function) works for me when I copy/paste it. -Dan Nope, I can remove the 5 and it's still the same. I am really stumped on this, could you look over it 1 more time, please, how is it working for you? you're not using arraY_keys like me though, try my function at home and see if it works. Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295386 Share on other sites More sharing options...
ManiacDan Posted December 7, 2011 Share Posted December 7, 2011 I copied and pasted your 2 lines of code (which again, is not a function) into a document and it worked. $txt = "I <3 PHP it's the best! :thumbsup: WOO!"; $smilies=array ( '#(?<!\w):thumbsup:(?!\w)#i' => '<img src=html/emoticons/thumbsup.gif alt=":thumbsup\:">', '#(?<!\w)<3(?!\w)#i' => '<img src=html/emoticons/heart2.gif alt="<3">', ); $txt=preg_replace(array_keys($smilies), array_values($smilies), $txt,5); echo $txt; This outputs: I <img src=html/emoticons/heart2.gif alt="<3"> PHP it's the best! <img src=html/emoticons/thumbsup.gif alt=":thumbsup\:"> WOO! There's not much more I can tell you. Are you looking at the HTML source? Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295402 Share on other sites More sharing options...
joe92 Posted December 7, 2011 Share Posted December 7, 2011 Not sure on solving this specific problem, however there is absolutely no point in over complicating your regex by using (?<!\w) and (?!\w). If you need to make sure your pattern is not contained within a word, use the word boundaries metacharacter. This makes sure it is matching a whole word only. Yours would become: '#\b<3\b#i' Also, the i modifier means case-insensitive. There is absolutely no point in using it for this pattern. A 3 does not have a lower or upper case, neither does a <. '#\b<3\b#' Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295405 Share on other sites More sharing options...
Monkuar Posted December 7, 2011 Author Share Posted December 7, 2011 Wow this is really frustrating, Thank you both for your replies, unfortunately it's still not going through. I even tried: '#\b=]\b#i' a smiley =] and it works fine but whenever I put in the < character it seems it does not work, it accepts every other character though, do you think i need to have something in php to enable something like this? I do have this code that is below this code: $txt = preg_replace("/&#([0-9]+);/s", "&#\\1;", $txt ); But I removed it just cause, and hell it's still not reading my "<3" I can use the LETTER "5" and it works, but not "<3" anything with "< or >" just messes it up and doesn't go through, I am simply dumbfounded Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295420 Share on other sites More sharing options...
Pikachu2000 Posted December 7, 2011 Share Posted December 7, 2011 You aren't using htmlentities or htmlspecialchars on the string before the preg_replace are you? Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295423 Share on other sites More sharing options...
ManiacDan Posted December 7, 2011 Share Posted December 7, 2011 Ok, you seem to be steadfastly using the wrong words here, but I'm going to skip right over that to repeat my question: ARE YOU LOOKING AT THE HTML SOURCE? Also, like Pikachu said, are you doing anything aside from what you said you've done (and what I've demonstrated)? HTML entities, escaping, str_replace, anything? I copied and pasted what you gave me and it worked. I'll repeat: I took what you posted. Character for character. Exactly zero changes. It worked. Also note that your INPUT might not contain the < character. THAT may be run through htmlspecialchars or something. Note that the < is hard-coded in my string. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295429 Share on other sites More sharing options...
Monkuar Posted December 7, 2011 Author Share Posted December 7, 2011 Ok, you seem to be steadfastly using the wrong words here, but I'm going to skip right over that to repeat my question: ARE YOU LOOKING AT THE HTML SOURCE? Also, like Pikachu said, are you doing anything aside from what you said you've done (and what I've demonstrated)? HTML entities, escaping, str_replace, anything? I copied and pasted what you gave me and it worked. I'll repeat: I took what you posted. Character for character. Exactly zero changes. It worked. Also note that your INPUT might not contain the < character. THAT may be run through htmlspecialchars or something. Note that the < is hard-coded in my string. -Dan Hey, I found this in some other code somewhere else: $val = str_replace( "<" , "<" , $val ); I guess this was the culprit.. wow Facepalm me, I was looking at the source and it had the characters < I was thinking to myself, does it auto convert into that normally? Doesn't it need a str_replace to get into that kind of code? (While viewing source), so I want to say Thank you pikachu for telling me about viewing source, I know I should have, and I will continue that in my future of coding, if I am stuck. I learned a big lesson here and I am thankful for you guys helping me. Wow, ManiacDan, so sorry for giving you a hard time, I bet you were like "WTF" Thanks again all, I am going to mark this as Resolved. I am extremely Happy. Quote Link to comment https://forums.phpfreaks.com/topic/252680-need-to-get/#findComment-1295441 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.