SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing string with case sensitivity issues

    I'm trying to grab a string inside a web page and am having issues with varying cases on the surrounding text. I think I want a regex function (preg_grep?), but I'm not sure which one or how to write it.

    Here is an example of the strings I'm trying to parse:
    Code:
    Part Number:</B> 1234atoz<BR><B>
    
    Part Number:</b> 1234atoz<br><b>
    I'm trying to grab just the "1234atoz" portion which can be any combination and length of alphanumerics. I want to maintain the case of the portion I'm grabbing, but ignore the case of the other parts. The html varies from upper case to lower case which is making my life difficult.

    Thanks for your help!

  2. #2
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assuming you already have your search expression, just use the /i modifier...?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is I am regex challenged. I have never used it in my php code before and don't even know where to start. I'm reading through tutorials and am trying to validate the code in a simulator, but it is not working.

  4. #4
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am NOT good with regex, but I would think something similar to:

    PHP Code:
    preg_match_all('/Part Number:<\/b> ([a-z0-9]+)<br>/i'$html$matches);
    $parts $matches[1];
    print_r($parts); 
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  5. #5
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's getting close. The /i doesn't seem to be working, but if I remove that part and change it to:

    Code:
    /Part Number:<\/b> ([a-zA-Z0-9]+)<br>
    it works with the lower case version. Any other ideas on getting the case independence working?

  6. #6
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whitemank View Post
    That's getting close. The /i doesn't seem to be working, but if I remove that part and change it to:

    Code:
    /Part Number:<\/b> ([a-zA-Z0-9]+)<br>
    it works with the lower case version. Any other ideas on getting the case independence working?
    Hmm... it works just fine for me? I get both part #s...
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  7. #7
    SitePoint Zealot
    Join Date
    Jan 2006
    Posts
    169
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm. Maybe my simulator isn't working right. I put your expression in directly:

    Code:
    preg_match_all('/Part Number:<\/b> ([a-zA-Z0-9]+)<br>/i', $page_text, $matches);
    $parts = $matches[0];
    print_r($parts);
    and this is what I get back

    Code:
    Array ( [0] => Part Number: 13013
    )
    It is leaving the Part Number: portion of the string.

  8. #8
    SitePoint Wizard triexa's Avatar
    Join Date
    Dec 2002
    Location
    Canada
    Posts
    2,476
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by whitemank View Post
    Hmmm. Maybe my simulator isn't working right. I put your expression in directly:

    Code:
    preg_match_all('/Part Number:<\/b> ([a-zA-Z0-9]+)<br>/i', $page_text, $matches);
    $parts = $matches[0];
    print_r($parts);
    and this is what I get back

    Code:
    Array ( [0] => Part Number: 13013
    )
    It is leaving the Part Number: portion of the string.
    ...Why not use it in a real-world example instead of through a simulator? Create a test.php, create or paste how it would be, then apply the code to that...?
    AskItOnline.com - Need answers? Ask it online.
    Create powerful online surveys with ease in minutes!
    Sign up for your FREE account today!
    Follow us on Twitter

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    preg_match_all('~Part Number:</b> ([^<]+)<~i', $subject, $result, PREG_PATTERN_ORDER);
    print '<pre>' . print_r($result, 1) . '</pre>';

    You should see what it returns before you try to do anything with it. When I do this I get:

    Code:
    Array
    (
        [0] => Array
            (
                [0] => Part Number:</B> 1234atoz<
                [1] => Part Number:</b> 1234atoz<
            )
    
        [1] => Array
            (
                [0] => 1234atoz
                [1] => 1234atoz
            )
    
    )


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
  •