PHP function to get part of string

Discussion in 'PHP' started by dsv10, Mar 4, 2011.

  1. #1
    Hello everyone...
    I need help with some PHP script i am working on - i need help to get the best way to do this:
    //this is what i get in the POST
    $full_number = '1234123121';

    //i need to make this as output:
    $number = '41';

    basically i am looking for some way, maybe using str_replace() to get the script to count 123 and output the first two charters after it... the full number is dynamic..

    Thanks.
     
    dsv10, Mar 4, 2011 IP
  2. mihaidamianov

    mihaidamianov Well-Known Member

    Messages:
    1,434
    Likes Received:
    111
    Best Answers:
    0
    Trophy Points:
    190
    #2
    mihaidamianov, Mar 4, 2011 IP
  3. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    $number = '12312312312312341123123123133';
    $numtofind = '41';
    
    $pos = strpos($numtofind, $number);
    
    $str = subtr($number, $pos, strlen($numtofind));
    echo $str;
    
    PHP:
    This finds the position of '41' in the string using strpos. It then uses substr to create a sub-string that is x characters long (based on the length of the string we're looking for) at the position of the search. :)
     
    Mike Griffiths, Mar 4, 2011 IP
  4. ACME Squares

    ACME Squares Peon

    Messages:
    98
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $number = substr($full_number, 3, 2);

    To future proof my answer, PHP 6 would be
    $number = $full_number[3,2];
     
    Last edited: Mar 4, 2011
    ACME Squares, Mar 4, 2011 IP
  5. dsv10

    dsv10 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Thanks for the fast replay... well... sorry i didnt explain it right :)

    the full number is dynamic but his length is 10-15 numbers...
    i need to output the 4th and 5th numbers from left only...
     
    dsv10, Mar 4, 2011 IP
  6. mihaidamianov

    mihaidamianov Well-Known Member

    Messages:
    1,434
    Likes Received:
    111
    Best Answers:
    0
    Trophy Points:
    190
    #6
    substr ($full_number,3,2)
     
    mihaidamianov, Mar 4, 2011 IP
  7. dsv10

    dsv10 Peon

    Messages:
    9
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    yes found it on php.net - thank you people! works great.
     
    dsv10, Mar 4, 2011 IP
  8. FCM

    FCM Well-Known Member

    Messages:
    669
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    155
    #8
    Can I ask what he is doing this for? Is it for encryption? I mean - what is the point in looking for that particular part of the string?




    }
    [/QUOTE]
     
    FCM, Mar 4, 2011 IP
  9. Mike Griffiths

    Mike Griffiths Peon

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Could be any number of things
     
    Mike Griffiths, Mar 5, 2011 IP