explode is not exploding as it should! [SOLVED]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mrbeatnik
Forum Newbie
Posts: 4
Joined: Thu Aug 11, 2005 8:47 am

explode is not exploding as it should! [SOLVED]

Post 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!
Last edited by mrbeatnik on Thu Aug 11, 2005 10:35 am, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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?
mrbeatnik
Forum Newbie
Posts: 4
Joined: Thu Aug 11, 2005 8:47 am

Post 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!
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post 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.)
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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
mrbeatnik
Forum Newbie
Posts: 4
Joined: Thu Aug 11, 2005 8:47 am

SOLVED!

Post 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..!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
Post Reply