Jump to content

smilies in php message board


almightyegg

Recommended Posts

i get an error...but i dont understand :(
Parse error: syntax error, unexpected '[', expecting ')' in /home/lordofth/public_html/forum/viewthread.php on line 78

[code]<?
    function emoticon($r['post']) {
  // the line below this is line 78
  $emoticonarray = array(
    ':)'  => 'smile.gif',
    ':('  => 'sad.gif',
    ';)'  => 'wink.gif',
    ':P'  => 'tongue.gif'
  );
    foreach($emoticonarray as $emoticon => $img) {
    $search[] = $emoticon;
    $replace[] = '<img src="/emoticons/' . $img . '" alt="' . $emoticon . '" />';
  }
  $r['post'] = str_replace($search, $replace, $r['post']);
  return $r['post'];
}

    echo stripslashes($r['username']); ?></td>
    <td style="width:80%;background-color:#101010;">
    <?
    echo nl2br(stripslashes($r['post'])); ?></td>
  </tr>
</table>[/code]
Link to comment
Share on other sites

Its actually to do with this:
[code=php:0]function emoticon($r['post']) {[/code]


You cannot use an array as parameter for tghe function, you can passa n array to the function when you call it. So remove the $r['post'] variable and replace it with $r_post or something similiar. Also make sure chnage any other instances of $r['post'] too in the emoticon function to $r_post too.

Then when you call the emotion function you can use $r['post']:
[code=php:0]// call the emoticon function
$r['post'] = emoticon($r['post']);[/code]
Link to comment
Share on other sites

didn't really understand that :(

could you rewrite this? then in future i will understand :)
[code]  function emoticon($r['post']) {
  // defines the emoticons
  $emoticonarray = array(
    ':)'  => 'smile.gif',
    ':('  => 'sad.gif',
    ';)'  => 'wink.gif',
    ':P'  => 'tongue.gif',
    ':'('  => 'cry.gif'
  );
  foreach($emoticonarray as $emoticon => $img) {
    $search[] = $emoticon;
    $replace[] = '<img src="images/emotions/' . $img . '" alt="' . $emoticon . '" />';
  }
  $r['post'] = str_replace($search, $replace, $r['post']);

  return $r['post'];
}

    echo stripslashes($r['username']); ?>[/code]
Link to comment
Share on other sites

You need to read up on creating your own functions if you are not understanding the code. Prehaps reading [url=http://dev.fyicenter.com/faq/php/php_function_definition.php]this[/url] may help yuou understand.

if you follow what I said IN my previous post your function should work. However the following is what your code should be:
[code]// we difine our function here
// PHP will not run this function until we tell it to
// $post is the paramter for the function.
function emoticon($post)
{
    // defines the emoticons
    $emoticonarray = array( ':)'  => 'smile.gif',
                            ':('  => 'sad.gif',
                            ';)'  => 'wink.gif',
                            ':P'  => 'tongue.gif',
                            ':('  => 'cry.gif'
                          );

    foreach($emoticonarray as $emoticon => $img)
    {
        $search[] = $emoticon;
        $replace[] = '<img src="images/emotions/' . $img . '" alt="' . $emoticon . '" />';
    }

    $post = str_replace($search, $replace, $post);

    return $post;
}

// We'll now tell PHP to run our emoticon function we defined above:
// we'll pass $r['post'] as a paramter to the emoticon function.
// The function will now converet any smilie symbols in to images
$r['post'] = emoticon($r['post']);[/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.