Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts

    Question Losing the + sign in a function

    When this function I have is run, it removes any + signs for some reason. I looked for where it's doing it and can't seem to find it (even used ctrl+f). If someone can help me figure out why this is happening, that would be great.

    PHP Code:
        //$p example = '<m t="2+2" u="123" i="0" />'
        //m = packet type
        //t = text
        //u = user id
        //i = unknown still
        
    function parse($p$handle true)
        {
            global 
    $bot;
            if(
    substr_count($p'>') > 1)
                
    $p explode('/>'$p);
            foreach((Array)
    $p as $p2)
            {
                
    $p2 trim($p2);
                if(
    $bot->debug)
                    echo 
    "->>\t $p2\n";
                if(
    strlen($p2) < 5) return;
                
    $type trim(strtolower(substr($p21strpos($p2.' '' '))));//packet type
                
    $p2 trim(str_replace("<$type",''str_replace('/>'''$p2)));//remove details so it is just the info to be parsed
                
    parse_str(str_replace('"'''str_replace('" ''&'str_replace('="''='str_replace('&''__38'$p2)))), $this->packet[$type]);
                foreach(
    $this->packet[$type] as $k => $v)
                {
                    
    $this->packet[$type][$k] = str_replace('__38''&'$v); //htmlspecial chars are protected instead of being parsed
                
    }
                if(
    $handle)
                    
    $this->handle($type$this->packet[$type]);
            }
        } 
    Also, if your wondering, this is for my chat bot (I've asked other questions for it here).

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    I'm fairly certain none of the other functions are needed. Some of them are just variables and when the handle function is called, the + sign is gone (I printed the packet variable for ['t'] and it was blank (or space) in place of the sign.

    Variables:
    $bot->debug = Tells whether to print the text in the command prompt
    The + sign still shows at this point
    $this->packet = Stores the packet

    The rest of the variables are set in the function.

    Again when function handle is called, I put print $p; on the first line (or second after globals, but doesn't matter) and the sign was gone, but on the echo "->>\t $p2\n"; line it is

    Edit:
    Narrowed it down to this line:
    PHP Code:
    parse_str(str_replace('"'''str_replace('" ''&'str_replace('="''='str_replace('&''__38'$p2)))), $this->packet[$type]); 

    Edit:
    Narrowed it down to the parse_str. Apparently the function is supposed to convert + to spaces so I'm working on a workaround for it.


    Edit:
    Fixed with str_replace('+', '__39', $str);
    Then again swapped back in the foreach
    Last edited by Dubz; 04-22-2012 at 09:39 PM.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,474
    Thanks
    63
    Thanked 537 Times in 524 Posts
    Quote Originally Posted by Dubz View Post
    Edit:
    Narrowed it down to the parse_str. Apparently the function is supposed to convert + to spaces so I'm working on a workaround for it.
    Thats because parse_str() also decodes url data. The urlencoded character of ' ' is + so when decoded it will replace with ' '.

    I'm sure you've probably already figured that out yourself so my post is really for other googlers.
    I can't really think of anything to write here now...

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    428
    Thanks
    18
    Thanked 26 Times in 26 Posts
    Quote Originally Posted by tangoforce View Post
    Thats because parse_str() also decodes url data. The urlencoded character of ' ' is + so when decoded it will replace with ' '.

    I'm sure you've probably already figured that out yourself so my post is really for other googlers.
    Yeah I found that out on php.net
    The str_replace seems to kind of bypass it right now so I'll just leave it like that


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •