Page 1 of 3 123 LastLast
Results 1 to 10 of 23

Thread: Regex extract sequence of a certain format from string

  1. #1
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default Regex extract sequence of a certain format from string

    I think this is possible, but it's beyond my ability with regex.

    What I want is to extract a certain pattern from a string.

    So if the pattern is "any number" then I want to extract it from any string:

    'this is a string123 test'
    That returns '123'.


    The specific pattern I am matching is a 10-digit hexadecimal code.
    This means: exactly 10 characters, each of which is 0-9a-f.

    I would like the function to take a string as input and return an array containing all possible matches.

    Let me know if that isn't clear.
    Last edited by djr33; 10-05-2010 at 02:35 PM.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    If the numbers are always all grouped together this should work.

    PHP Code:
    $string "this is a string123 test";
    $numbers preg_replace('/.*?(\d+).*/''$1'$string);
    echo 
    $numbers
    Actually on rethinking this you don't even need a regex.

    You can just set it as an integer and all the text will be removed.

    Code:
    settype($string, "integer");
    Last edited by bluewalrus; 10-04-2010 at 06:21 PM.
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    djr33 (10-05-2010)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    [\dABCDEF]{10}

    untested.

    I think [0-9A-F]{10} would work equally well, and [0-9A-Fa-f]{10} if you might have lowercase hex numbers.

  5. The Following User Says Thank You to traq For This Useful Post:

    djr33 (10-05-2010)

  6. #4
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Could I have a better example of some of the text that you need processed? For example the following would work:

    PHP Code:
    <?php
    $css 
    "this is a test:ade40275ad tadaa.bde40275ad ade40275ad ade40275ad";
    preg_match_all('/[a-fA-F0-9]{10}/s'$css$match);
    print_r($match);
    ?>
    resulting in:
    Code:
    Array ( [0] => Array ( [0] => ade40275ad [1] => bde40275ad [2] => ade40275ad 
    [3] => ade40275ad ) )

    I suspect that the parameters that you are working with are somewhat more complicated than that.
    Last edited by james438; 10-05-2010 at 02:13 AM. Reason: grammar
    To choose the lesser of two evils is still to choose evil. My personal site

  7. The Following User Says Thank You to james438 For This Useful Post:

    djr33 (10-05-2010)

  8. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Bluewalrus, thanks for the post, but I need this for hexadecimal not just regular integers. I like that creative method though. It actually might be possible if I set the format of the number of hexadecimal then try that... not sure if PHP is capable of ordering it that way though.

    Adrian and James, thanks! One of those will work, I'm sure.
    Yes, it might be lowercase or uppercase.
    No, it shouldn't be much more complex than that.

    I'm creating a verification system so that users must reply to an email and the subject line contains a verification sequence (a 10 digit portion of an md5-generated hash string). The email will be sent to them. All they need to do is reply. In most cases the subject line will still be "{seq}" or "RE: {seq}" but I don't want it to fail if the user does something unexpected. Additionally, if there is something wrong with the email sent to them or they want to reply from a different account, they might type it manually in a different email.

    The purpose of this is to create an email based uploading system so that users on mobile devices can attach an image or audio clip to an email sent to my server [an automatically managed email address/account] that then will check incoming messages and "upload" attachments to the server. Of course I don't want just any files getting through since users must be registered, so I will give them a 'key' for each 'upload' and they just need to include that somewhere in the subject line of the email.

    Very complex to figure out, but worth it because it's pretty easy for the user and in many cases mobile browsers don't have the ability to do a normal file upload. For example, the iphone. But it can attach a file to an email and send it, so that's all that needs to be done here.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  9. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    cool... I'd actually be interested in how you're checking the email and uploading the attachments from it.

  10. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm using the IMAP functions to connect to an email account.

    The system isn't polished yet, but it is functional. If you want to see some specific code, contact me directly.

    It's not really something I can easily post here since it exists in several pieces (and I wouldn't want to until it's worked out a bit more).

    The hardest part was simply figuring out how to deal with the mail server. The rest is just step by step in PHP-- not "easy" exactly, but a lot easier than figuring out the mail server and connecting to it.

    Another problem is that it either needs to run as a cron job or be triggered by the user, and either way it's not fast: about a 3 second delay to check and parse the messages (connection time, not transfer time for files-- that's fast usually, not noticable).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  11. The Following User Says Thank You to djr33 For This Useful Post:

    traq (10-06-2010)

  12. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    hmm.

    I've had a passing curiosity about emailing stuff to my website (like Facebook mobile allows). I'll look into IMAP, and drop you a note if I want to know more.

    thanks

  13. #9
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    It's all very possible, but as I said, the biggest problem becomes the cron job part of it. It's easy to send an email because you have a triggering event (user doing something) but checking for emails ends up with the same problem that creating a chat script does: basically you need to constantly refresh. Cron jobs may be the answer, but they're not efficient for the server.
    Let me know how it goes. I've been curious about this for a while too but never had a specific need to try it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  14. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    well, I may not provide an answer. I'd probably be happy with using my login as a trigger to check for emails.

    (actually, that's a hacky solution, but it would probably work pretty well - every time you serve a page to a logged-in user, check that user's "inbox." I think you could use ob_start() + ob_flush() to effectively do it in the background -serve all the page content first, then deal with the email connection... serve a little bit of ajax to wait for the response, or save it to the session and display on the next page load.)

    Edit:

    I may be completely wrong, however.


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
  •