Page 1 of 1

strlen() returns an unexpected number

Posted: Mon Jun 29, 2009 12:26 pm
by tempomental
I'm trying to parse the following string:

API_Authenticate 0 No error 4_beiz23n4u_bymy8i_b_cbyyzg4wwerpnd34kp4hd4kiakf_b737vy85rv2ge_ 57007048.d2m4

(This is the string returned from a call to file_get_contents that references a URL that returns XML -- i.e. file_get_contents(http://foo.com?etc..), where directly referencing http://foo.com?etc would normally return XML))

The value I'm interested in is the 4_beiz23n4u...; I need to extract that from the string (it gets passed as a parameter in a different URL).

Unfortunately, the PHP I'm using doesn't seem to be behaving how I expect... which means, I'm probably doing it wrong. :-)

here's what I'm doing:

Code: Select all

$ret = file_get_contents('[i]url[/i]');
 
list($action, $errcode, $errtext, $ticket) = explode(" ", $ret);
 
$ticked = ltrim($ticket, "ero "); // necessary to trim "error" from front of string
echo "Strlen(Ticked) is ".strlen($ticked)."<br />";
$ticked2=rtrim($ticked);
echo "Strlen(Ticked2) after rtrim() is ".strlen($ticked2)."<br />";
$ticked3 = substr($ticked2, 0, -15);
echo "Ticked3 is ".$ticked3."<br />";
What's happening is this: $ret is returning the string at the beginning of the post ("API_Authenticate...") just fine. (Line 1)
explode() does not seem to work as advertised, though. It is lumping "error 4_beiz23n4u_bymy8i_b_cbyyzg4wwerpnd34kp4hd4kiakf_b737vy85rv2ge_ 57007048.d2m4 " into the $ticket variable. I've tried adding other variables to list() and I get the same result, strangely). (Line 3)
ltrim() (Line 5) works OK, but it tells me the resulting string has a strlen() of 139, hence the call to rtrim().
Rtrim() (Line 7) does nothing noticeable; the strlen() after rtrim() is still 139.
This renders the call to substr() useless, unfortunately. (Line 9)

So... my dilemma (and apologies in advance for the horrid variable names): how do I get that huge string I need out of the original result from file_get_contents()? I'm stumped here as to why it's not behaving how I want it to. ;-)

Thanks in advance!

Re: strlen() returns an unexpected number

Posted: Mon Jun 29, 2009 12:31 pm
by Mark Baker
Start by considering that the error message "No error" contains a space, then reconsider your explode()

Re: strlen() returns an unexpected number

Posted: Mon Jun 29, 2009 12:34 pm
by tempomental
Mark Baker wrote:Start by considering that the error message "No error" contains a space, then reconsider your explode()
Mark,

I already have, by adding a variable $errtext2 after $errtext in the list(). What happened is that "error" and everything after wound up dumped in the $errtext2 variable.

tm

Re: strlen() returns an unexpected number

Posted: Mon Jun 29, 2009 12:54 pm
by Mark Baker
In that case, check whether the space between error and 4_beiz23n4u etc really is a space, or some other invisible character such as a tab

Re: strlen() returns an unexpected number

Posted: Mon Jun 29, 2009 1:10 pm
by tempomental
Mark Baker wrote:In that case, check whether the space between error and 4_beiz23n4u etc really is a space, or some other invisible character such as a tab
According to Notepad++, it's just a space. Odd.

EDIT: A-ha! Dumping the string I was getting from file_get_contents() using bin2hex() shows that there is an xml header in there that wasn't being displayed, and was therefore mucking things up. Accounting for that seems to have cleared up the problem.

Thanks for your help!