Jump to content

data parse isn't working correctly


ober

Recommended Posts

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]<?php
function 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?
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

Allow me to post the final code to better explain:
[code]<?php
function 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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.