TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   This stupid str_replace bug? (http://www.talkphp.com/general/2202-stupid-str_replace-bug.html)

Nor 02-06-2008 05:31 PM

This stupid str_replace bug?
 
str_replace bug in PHP 5.2.4?

I got this function right:

Code:

function getEmoticons( $key, $post )
{
        global $mysqli;
        $query = $mysqli->query("SELECT * FROM `zbemoticons` WHERE `userkey` = '{$key}'");
        if( $query )
        {
                $q = array();
                while( $fetch = $query->fetch_array() )
                {
                        $q[] = $fetch;
                }
                for($x=0;$x<count($q);$x++)
                {
                        $image = '<' . 'img' . ' src='. '\'' . $q[$x]['image']. '\'' . '/' . '>';
                        $text = $q[$x]['text'];
                        $post = str_replace( $text, $image, $post );
                        //$post = str_replace( trim($q[$x]['text']), trim($q[$x]['image']), $post );
                }
                return $post;
        }
        else
        {
                return $post;
        }
}

As you might notice you would simply think the image is parsed correctly?

This is the outcome:

Code:

shouts[n++] = "<a href='http://s1.zetaboards.com/uStore/profile/31283'>Nor</a> -- <span style='color: black'> <img src='http://<img src='http://norvisions.info/emoticons/evilgrin.gif'/>visions.info/emoticons/hehe.gif'/> </span> "
How come the stupid image is being replaced incorrectly?

This is how the info is being displayed:

Code:

while( $fetch = $query->fetch_array() )
                {
               
                        //ukey($fetch['key'],$fetch['content']);
                        echo
                        ("

shouts[n++] = \"<a href='http://" . getDomain( $fetch['key'] ) . "profile/" . $fetch['userid'] . "'>" . $fetch['username'] . "</a> -- <span style='color: " . $fetch['color'] . "'> " . getEmoticons($fetch['key'],$fetch['content']) . " </span> \"       
                        ");
                }

Its a shoutbox, but I'm still getting confused on why the output is very inaccurate? Is this because of php 5.2.4?

Example SQL result looks like so:

Code:

CREATE TABLE `zbemoticons` (
  `id` int(255) NOT NULL auto_increment,
  `userkey` varchar(255) NOT NULL,
  `text` varchar(255) NOT NULL,
  `image` longtext NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

--
-- Dumping data for table `zbemoticons`
--

INSERT INTO `zbemoticons` VALUES(1, '2e64da0bae6a7533021c760d4ba5d6', ']-)', 'http://norvisions.info/emoticons/devlish.gif');
INSERT INTO `zbemoticons` VALUES(2, '2e64da0bae6a7533021c760d4ba5d6', 'hehe', 'http://norvisions.info/emoticons/hehe.gif');
INSERT INTO `zbemoticons` VALUES(3, '2e64da0bae6a7533021c760d4ba5d6', 'B-)', 'http://norvisions.info/emoticons/cool.gif');
INSERT INTO `zbemoticons` VALUES(4, '2e64da0bae6a7533021c760d4ba5d6', 'nor', 'http://norvisions.info/emoticons/evilgrin.gif');

So I don't see how its even getting messed up. Note I had it using just the whole look before adding the fetch to the db, and I noticed that way and the current way I'm selecting the data isn't the problem.

This is getting retarded -.-..

Was debugging: Ustore

wGEric 02-06-2008 05:42 PM

There is part of the last emoticon 'nor' in the URL.

Code:

http://norvisions.info/emoticons/hehe.gif
So as other emoticons get added when it gets to the last one it replaces 'nor' in norvisions.info with it.

str_replace probably isn't going to work. I'd suggest using reg ex and require that emoticons can't be in the middle of words.

Nor 02-06-2008 05:49 PM

oh snap, that could be possible :O.. lemme check

Alright how I could I use reg ex to do the same thing? RegEx is my weak point in PHP which sucks..


All times are GMT. The time now is 05:33 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0