Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post

    substr.. no value returned?

    Why am I getting no value returned on this?

    PHP Code:
    $paid_id substr($rp_id9$ranlen); 
    Thanks!

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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.

    PHP Code:
    $paid_id substr($rp_id9$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.

  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ok...

    PHP Code:

    $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_id9$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!

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Well the 3 argument in supstr is optional. It will give you the part of the string starting at argument 2 to the end.

  • #5
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Ahh ok.. so I could just go like this:

    PHP Code:
    $paid_id substr($rp_id9); 
    .. & get all characters from 9 onwards returned?

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Yes. You could alternativley try it yourself instead of asking.

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Well I tried that & still got no value returned..... what's wrong here?

  • #8
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $s 1234567890;
    echo 
    substr($s9); 
    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.

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by cyphix
    Well I tried that & still got no value returned..... what's wrong here?
    It is the nineth in your looking for correct? Not like, the first nine of the string?

  • #10
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    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".

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,980
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Oh, then in that case just change it to:
    PHP Code:
    $paid_id substr($rp_id8); 
    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:
    PHP Code:
    $paid_id substr($rp_id8strlen($paid_id));
    // Would return all characters >= the 9th on.

    $str substr($string0strlen($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";
    */ 

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Thanks! That worked!

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

    PHP Code:
    $string 1234567890;

    $str1 substr($string08); // Returns "12345678"

    $str2 substr($string8); // 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

  • #13
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    When it reaches the 8th character it stops. So, 12345678 should be the right ouput for the first one.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •