...

View Full Version : Losing the + sign in a function



Dubz
04-22-2012, 07:34 AM
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.



//$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($p2, 1, strpos($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).

Dubz
04-22-2012, 07:50 PM
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


Narrowed it down to this line:


parse_str(str_replace('"', '', str_replace('" ', '&', str_replace('="', '=', str_replace('&', '__38', $p2)))), $this->packet[$type]);



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.



Fixed with str_replace('+', '__39', $str);
Then again swapped back in the foreach

tangoforce
04-23-2012, 08:33 PM
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.

Dubz
04-24-2012, 01:26 AM
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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum