ober Posted January 12, 2007 Share Posted January 12, 2007 So I have a chat feature on another SMF board and I'm trying to parse smiley's into it. The display portion is a div, so I just need to replace certain collections of characters with <img tags. Here is my code:[code]<?phpfunction parse_data($data){ $mod_data = explode(' ', $data); for($i=0;$i<count($mod_data);$i++) { if(strpos($mod_data[$i], 'http://') === 0 && strlen($mod_data[$i]) > 10) $mod_data[$i] = substr($mod_data[$i], 7); if(strpos($mod_data[$i], 'www') === 0 || strpos($mod_data[$i], '.com') > 0 || strpos($mod_data[$i], '.net') > 0) $mod_data[$i] = '<a href="http://' . $mod_data[$i] . '" target="_blank">' . $mod_data[$i] . '</a>'; if(trim($mod_data[i]) == ":)") $mod_data[$i] = '<img src="Smileys/default/smile.gif" />'; if(trim($mod_data[i]) == ":(") $mod_data[$i] = '<img src="Smileys/default/cry.gif" />'; } $data = implode($mod_data, ' '); return $data;}?>[/code]I basically store 20 responses in a DB and as I display them, I send the output to this function. For whatever reason, it's not reading the :) and replacing it with the img tag. Can someone spot what I'm doing wrong? Quote Link to comment Share on other sites More sharing options...
utexas_pjm Posted January 12, 2007 Share Posted January 12, 2007 Don't make things so hard on yourself.[code]<?php $mod_data =str_replace(":)", '<img src="Smileys/default/smile.gif" />', $mod_data); //... etc?>[/code] Quote Link to comment Share on other sites More sharing options...
ober Posted January 12, 2007 Author Share Posted January 12, 2007 Well, I guess I'll use that with one modification:[code]<?php $mod_data =str_replace(" :) ", ' <img src="Smileys/default/smile.gif" /> ', $mod_data); //... etc?>[/code]To avoid instances where that string is not alone.I still don't understand why it wasn't working otherwise. Quote Link to comment Share on other sites More sharing options...
taith Posted January 12, 2007 Share Posted January 12, 2007 $mod_data[i] --> $mod_data[$i] # you forgot the $'s Quote Link to comment Share on other sites More sharing options...
ober Posted January 12, 2007 Author Share Posted January 12, 2007 Yet a better version:[code]<?php$lookfor = array(" :)", " :) ", ":) ");$data = str_replace($lookfor, ' <img src="Smileys/default/smile.gif" /> ', $data);$lookfor = array(" :(", " :( ", ":( ");$data = str_replace($lookfor, ' <img src="Smileys/default/cry.gif" /> ', $data);?>[/code]And taith, he didn't forget the $i's... the code he showed would go outside the for loop after it's been imploded again. Quote Link to comment Share on other sites More sharing options...
taith Posted January 12, 2007 Share Posted January 12, 2007 ... no... you really did... [code]if(trim($mod_data[i])[/code] Quote Link to comment Share on other sites More sharing options...
ober Posted January 12, 2007 Author Share Posted January 12, 2007 Allow me to post the final code to better explain:[code]<?phpfunction parse_data($data){ $mod_data = explode(' ', $data); for($i=0;$i<count($mod_data);$i++) { if(strpos($mod_data[$i], 'http://') === 0 && strlen($mod_data[$i]) > 10) $mod_data[$i] = substr($mod_data[$i], 7); if(strpos($mod_data[$i], 'www') === 0 || strpos($mod_data[$i], '.com') > 0 || strpos($mod_data[$i], '.net') > 0) $mod_data[$i] = '<a href="http://' . $mod_data[$i] . '" target="_blank">' . $mod_data[$i] . '</a>'; } $data = implode($mod_data, ' '); $lookfor = array(" :)", " :) ", ":) "); $data = str_replace($lookfor, ' <img src="Smileys/default/smile.gif" /> ', $data); $lookfor = array(" :(", " :( ", ":( "); $data = str_replace($lookfor, ' <img src="Smileys/default/cry.gif" /> ', $data); return $data;}?>[/code] Quote Link to comment 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.