View Full Version : substr.. no value returned?

10-13-2004, 04:23 PM
Why am I getting no value returned on this?

$paid_id = substr($rp_id, 9, $ranlen);


10-13-2004, 05:59 PM
You do not have enough information in it.

substr returns just the section that you have specified of a string. It may return false because either of your variable elements may be empty.

$paid_id = substr($rp_id, 9, $ranlen);

The problem is, without knowing the $rp_id I don't know what you're looking for. Say its some numbers, $rp_id = '10000000';.
Now, assuming that the $ranlen is for a random length, which unfortunatly I don't know how your obtaining it, it could be anywhere from {-i, i}. But lets say its anywhere from {-1,10}.
Now, if its say that it is -1. Your $paid_id will return 1000000. If its say 10, your $paid_id will return nothing.
Please be more specific as to what its for, as there may be an alternate way around it.

10-13-2004, 06:39 PM

$rp_id = 1234567890; // In reality this number can vary from 9 chars & upwards

$ranlen = strlen($rp_id); // Get the string length of it

$paid_id = substr($rp_id, 9, $ranlen); // I want to get the string that starts @ char 9 right up until the end (hence I use $ranlen to know what the last char is)

Thanks for the help! :)

10-13-2004, 06:48 PM
Well the 3 argument in supstr is optional. It will give you the part of the string starting at argument 2 to the end.

10-13-2004, 07:21 PM
Ahh ok.. so I could just go like this:

$paid_id = substr($rp_id, 9);

.. & get all characters from 9 onwards returned?

10-13-2004, 07:47 PM
Yes. You could alternativley try it yourself instead of asking.

10-13-2004, 10:05 PM
Well I tried that & still got no value returned..... what's wrong here? :mad:

10-13-2004, 10:26 PM
$s = 1234567890;
echo substr($s, 9);
Outputs 0 for me, which is right.

If you are using that value in an if statement (or similair) 0 amounts to false. that may be (but may not be,) your problem.

10-14-2004, 05:29 AM
Well I tried that & still got no value returned..... what's wrong here? :mad:

It is the nineth in your looking for correct? Not like, the first nine of the string?

10-14-2004, 08:48 AM
Outputs 0 for me, which is right.

Shouldn't it output "90" from that example seeing as the number 9 is the 9th char in the string so it should start from there?

It is the nineth in your looking for correct? Not like, the first nine of the string?

Correct.... in the example above I would want the output to be "90".

10-14-2004, 07:20 PM
Oh, then in that case just change it to:

$paid_id = substr($rp_id, 8);

substr strips starting and including the start character. Say you wanted to start from the first character, you cannot use 1, you would need to use 0. And though useless, unless you wanted to strip more off of it, you could use more for an ending variable as well:

$paid_id = substr($rp_id, 8, strlen($paid_id));
// Would return all characters >= the 9th on.

$str = substr($string, 0, strlen($string) - 2);
/* My personal favorite, its used when string is looped for comma seperated information. $string = "one, two, three, four, "; This will so nicely remove the last comma from the string so it looks nice: $str = "one, two, three, four";

10-15-2004, 11:23 AM
Thanks! That worked! :thumbsup:

However, I'm intrigued by how it works. as if "0" is indeed the first number in the string counted why when I use:

$string = 1234567890;

$str1 = substr($string, 0, 8); // Returns "12345678"

$str2 = substr($string, 8); // Returns "90"

Shouldn't $str1 return "1234567" if it starts @ 0? In the above example how can char 8 be the character "8" @ the end of $str1 & then be the char "9" in $str2.. I'm confused :confused:

10-15-2004, 11:55 AM
When it reaches the 8th character it stops. So, 12345678 should be the right ouput for the first one.