Page 1 of 1

explode is not exploding as it should! [SOLVED]

Posted: Thu Aug 11, 2005 8:58 am
by mrbeatnik
Hi all. Hope you can help, this is driving me crazy...

Working: Hard coded string.

Code: Select all

$mess="just testing";
$p1mess = explode(' ', $mess);
This works fine, resulting in:

Code: Select all

$mess = just testing
$p1mess[0] = just
$p1mess[1] = testing

Not Working: String passed in packet.

Code: Select all

$mess=$this->frominc; //(retreive "just testing")
$p1mess = explode(' ', $mess);
This does not works at all, resulting in:

Code: Select all

$mess = just testing
$p1mess[0] = 
$p1mess[1] =
$this->frominc is a being intercepted from a packet. I have a feeling it might be arriving as XML. If I echo $mess, it shows the complete string, but it is NOT separating with the ' ' (space).


Working: String passed in packet.

Code: Select all

$mess=$this->frominc; //(retreive "just1testing")
$p1mess = explode('1', $mess);
This works fine, resulting in:

Code: Select all

$mess = just1testing
$p1mess[0] = just
$p1mess[1] = testing

Can anyone help out?
I need to explode on ' ' (space).

Thanks!

Posted: Thu Aug 11, 2005 9:03 am
by timvw
There is nothing wrong with explode...

There must be something wrong with your

Code: Select all

$mess=$this->frominc;
You could echo it... before you pass it to explode.. So you know for sure what you are exploding.

Btw, it's possible that the string contains a tab ("\t") instead of a space. Might also explain the weird behaviour.

Posted: Thu Aug 11, 2005 9:05 am
by nielsene
As a long shot, you might want to see if the arriving packet is using some nonstandard encoding. Perhaps the space is not getting encoded to its normal codepoint?

Posted: Thu Aug 11, 2005 9:12 am
by mrbeatnik
I'm convinced that the space is not being used as "normal", since it can correctly explode the string that uses the "1" other than " "(space).

When I echo the $mess, it gives it exactly as it should be. Of course, the webpage doesn't always display special chars (\t). I will check for that. I'm sure it's XML that is being returned....

"<body>just testing</body>" is the message part of the packet.
Will check the \t issue, any other suggestions would be lovely however :)

Thanks people!

Posted: Thu Aug 11, 2005 9:17 am
by nielsene
try using

Code: Select all

$mess=urldecode($mess);
That would at least change %32 back to space (which the browser would also do masking the problem.)

Posted: Thu Aug 11, 2005 9:20 am
by feyd
try showing it with:

Code: Select all

echo '<pre>'.htmlentities(var_export($mess,true),ENT_QUOTES).'</pre>';

// could also try
echo '<pre>'.var_export(rawurlencode($mess),true).'</pre>';
or variants thereof

SOLVED!

Posted: Thu Aug 11, 2005 10:35 am
by mrbeatnik

Code: Select all

echo '<pre>'.htmlentities(var_export($mess,true),ENT_QUOTES).'</pre>';
Produces:

Code: Select all

'<message from=\'blah@blah.com\' to=\'blah1@blah.com\' id=\'1345\' type=\'chat\'><body>just testing</body><x xmlns=\'jabber:x:event\'><composing/></x></message>'
As I thought, the <body> tags are still being caught, so...

Code: Select all

$newmess = explode('<body>', $mess);
$new1mess = explode('</body>', $newmess[1]);
$p1mess = explode(' ', $new1mess[0]);
Which gives:

Code: Select all

$p1mess[0]=just
$p1mess[1]=testing
Can't believe I didn't already try that...
Thanks for the help everyone!

Just one thing tho, is there a quicker piece of code to do what I did in those three lines? Not that it matters too much..!

Posted: Thu Aug 11, 2005 10:53 am
by feyd
if you used the xml functions/classes in php, it could make life a lot easier..

there's of course regular expressions that can retrieve the data too..