SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Thread: strpos problems

  1. #1
    SitePoint Member
    Join Date
    Jul 2008
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    strpos problems

    I am trying to use strpos to take a 6 digit string and break into
    to 2 3 digit strings.

    For example I need take $string = "846153"
    and break it into

    $first_3_digits = "846"
    $last_3_digits = "153"

    How can this be done with strpos? Or am i just using the wrong function?

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can always read the documentation for functions if you're not sure how they work, or what they do. www.php.net

    substr() is what you want.

  3. #3
    SitePoint Evangelist
    Join Date
    Jun 2006
    Location
    Wigan, Lancashire. UK
    Posts
    523
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    str_split()
    ---
    Development Projects:
    PHPExcel
    PHPPowerPoint

  4. #4
    SitePoint Guru risoknop's Avatar
    Join Date
    Feb 2008
    Location
    end($world)
    Posts
    834
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't understand why and how you would use strpos() here. Just do this:

    PHP Code:
    $string '846153';

    echo 
    substr($string0strlen($string)/2); 

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2008
    Posts
    48
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    substr() would be a good choice...and you can also use a simple loop for this.

    <?php
    $string = "123456";

    $string1= array();
    $string2=array();

    for($i=0; $i<3; $i++){
    string1[]=$string[i];
    }

    for($i=3; $i<6;;$i++){
    string2[]= $string[i];
    }
    ?>

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    A loop without limits as to how many characters:
    PHP Code:
    <?php
    function split_string($string$chunk_size){
        
    $chunks = array();
        
    $length ceil(strlen($string) / $chunk_size);
        for(
    $i 0$i $length$i++){
            
    $chunks[] = substr($string$i $chunk_size$chunk_size);
        }
        return 
    $chunks;
    }
    echo 
    '<pre>';
    var_dump(split_string('abcdefghijklmnopqrstuvwxyz'3));
    echo 
    '</pre>';
    Or, on the other hand, if you want to split it into a certain amount of items:
    PHP Code:
    <?php
    function split_string($string$array_length){
        
    $chunks = array();
        
    $chunk_size ceil(strlen($string) / $array_length);
        for(
    $i 0$i $array_length$i++){
            
    $chunks[] = substr($string$i $chunk_size$chunk_size);
        }
        return 
    $chunks;
    }
    echo 
    '<pre>';
    var_dump(split_string('abcdefghijklmnopqrstuvwxyz'3));
    echo 
    '</pre>';
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

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