SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast nosnevel's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match() help, please

    I have a data string that starts with a combination of upper/lower case letters, numbers, hyphen and period. The length of this part of the string is quite variable, do I'd like to use preg_match() to capture just this first portion of the string.
    <code>
    $string = 'IN-A-103P-C Fly, C. S. Indian Camp; 1886 1886 C.S. Fly Photographer HP Photographs ina103pc.jpg Apache Indians';

    if(preg_match("/^[-a-zA-Z0-9.]$/", $string, $matches)) {
    echo $matches[0];
    }
    </code>

    What I want the above code to return is IN-A-103P-C, stopping at that first whitespace, which is something besides a " ".

    Please show me what I need to do to make the preg_match solution work.

    Thanks!
    Larry Levenson
    Sigma Web Technologies - Prescott Valley, AZ, USA
    http://www.SigmaWebTechnologies.com

  2. #2
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Why not use explode if it will always be at the first portion of the string followed by a space?

    Code:
    $string = 'IN-A-103P-C Fly, C. S. Indian Camp; 1886 1886 C.S. Fly Photographer HP Photographs ina103pc.jpg Apache Indians';
    $exploded_str = explode(' ', $string);
    echo $exploded_str[0];

  3. #3
    SitePoint Enthusiast nosnevel's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=centered effect;4955588]Why not use explode if it will always be at the first portion of the string followed by a space?

    Tried that, but that space isn't a " ", it's some other kind of whitespace. Since I can't figure out what kind of whitespace it is, it seems simple to do the pre_match() method. Then simply stop when a character is not A-Z, a-z, o-9, or -, or a period.
    Larry Levenson
    Sigma Web Technologies - Prescott Valley, AZ, USA
    http://www.SigmaWebTechnologies.com

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,031
    Mentioned
    152 Post(s)
    Tagged
    2 Thread(s)
    Just remove the $ at the end of your pattern. The $ means "the end of the match must be at the end of the string". You don't want this, so you can remove that. You should leave the ^ though, because you want the start of the match to be at the start of the subject string
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Addict
    Join Date
    Dec 2005
    Posts
    336
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by nosnevel View Post
    Quote Originally Posted by centered effect View Post
    Why not use explode if it will always be at the first portion of the string followed by a space?
    Tried that, but that space isn't a " ", it's some other kind of whitespace. Since I can't figure out what kind of whitespace it is, it seems simple to do the pre_match() method. Then simply stop when a character is not A-Z, a-z, o-9, or -, or a period.
    Sorry, I should have read that the first time. You could also replace the white space with a space as well, then explode it.
    $string = preg_replace("'\s+'", ' ', $string);

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2011
    Posts
    69
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this
    PHP Code:
    $string 'IN-A-103P-C Fly, C. S. Indian Camp; 1886 1886 C.S. Fly Photographer HP Photographs ina103pc.jpg Apache Indians';
    list(
    $code) = preg_split('#[^a-z0-9\-]#si'$string2);
    echo 
    $code

  7. #7
    SitePoint Enthusiast nosnevel's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works great, Gvre. Thank you!
    Could you take a moment to explain in plain English what's happening here?? Then I'll have a better understanding for next time I run into a similar issue.

    Larry Levenson
    Sigma Web Technologies - Prescott Valley, AZ, USA
    http://www.SigmaWebTechnologies.com

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    69 Post(s)
    Tagged
    0 Thread(s)
    gvre's script reads:

    "Split the variable $string, using anything that isnt a-z,0-9,\, or - as a delimiter, and returning at most 2 parts (IE: Only do the first such split.)."

  9. #9
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    So what is the space character in this particular case then? How would the OP go about finding that out?

  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,396
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    So what is the space character in this particular case then? How would the OP go about finding that out?
    An easy way would be to show us the output of echo base64_encode($string). At least that way we can determine the actual contents, which would be a great start.
    Salathe
    Software Developer and PHP Manual Author.

  11. #11
    SitePoint Enthusiast nosnevel's Avatar
    Join Date
    Aug 2003
    Location
    US
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks StarLion! Very helpful
    Larry Levenson
    Sigma Web Technologies - Prescott Valley, AZ, USA
    http://www.SigmaWebTechnologies.com


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
  •