SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Howdly doodley.....

    Does anyone know the most efficient way to find the 1st occurrence of an integer within a string please.

    e.g. $string = "grgegeegegeeg1000grggrrergege";
    ...and we ant to find the position of 1. But this must be generic - strpos($string, "1") is not good enough bcs on the next string the 1st integer maybe a 2. So will I need to loop through all possible integers?

    Gettype won't work in this scenario, bcs the integer will be thought of as a string. Therefore the only other way round this I can think of is to use ASCII and the ord function.
    But this will involve quite a lengthy function, to chop each character and check it's ASCII code.

    So is there a quicker way please - maybe using ereg or something?

    Cheers,

    Jason

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Jason I came up with this, I know it seems like a pain but I think it may be the only way, other options are certianly welcomed.

    PHP Code:
    function find_int($str) {
        
    $arr = array("1","2","3","4","5","6","7","8","9","0");
        
    $count 0;
        
    $x 0;
        for(
    $i=0;$i<=strlen($str);$i++) {
            if (
    in_array(substr($str$i1), $arr)) {
                
    $dude $i;
                break;
                }
            else {
                continue;
                }
            }
        return 
    $dude;
        }

    $str "dsajkfhds3fjkhsdfgh24hkjhsh245hjkh";

    print 
    find_int($str); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yup - thanks Freddy!!!

    This was the method I had in mind also - I was just hoping that there may be a faster way.

    Cheers......


    Jason

  4. #4
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is mine:

    PHP Code:
    $string "grgegeegegeeg1000grggrrergege";
    if (
    ereg("^[^0-9]*([0-9])",$string,$match))
    $pos strpos($string,$match[1]);
    }
    echo(
    $pos); 
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  5. #5
    SitePoint Zealot
    Join Date
    Jun 2000
    Posts
    165
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you son - I'll have to run some speed checks, and discover which method is the quicker!


    Cheers.....


    Jason

  6. #6
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I doubt you could see a big difference for small string (or small number of strings you need to do this) But I'm postive that mine work faster. Freddy, maybe you just didn't think of this algorithm at that moment.
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That and I find regular expressions to be evil
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Regular expressions are pure evil... That's why we love 'em!
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference


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
  •