Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts

    multiple pregmatch

    hi im trying to do a pregmatch and have it run through multiple values and need some help.
    I tried this right here.
    PHP Code:
    preg_match('/p([\d][\d]?)/'|'/r\.([\d][\d]?)/'|'/z\.([\d][\d]?)/',$t,$m); 
    and this, and it doesnt work.
    PHP Code:
    preg_match('/p([\d][\d]?)/','/r\.([\d][\d]?)/','/z\.([\d][\d]?)/',$t,$m); 
    do you see and understand what im trying to do? can someone help?
    Last edited by cosmicsea; 03-18-2010 at 01:16 AM. Reason: typo

  • #2
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Post an example of what you're trying to match.

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Post an example of what you're trying to match.
    well this is for a crawler script and im trying to make it loop through multiple values instead of "original code"
    PHP Code:
    preg_match('/p([\d][\d]?)/',$t,$m); 
    I want to know if i can add more to the preg_match so that it only loops through 1 time instead of several instances of preg_match.

    what that code is trying to match is the letter p and any random 2 numbers. for example "p12" and it works fine, i just want to add a couple more things for it to match like
    PHP Code:
    '/r\.([\d][\d]?)/' 
    which would be "r.12" or whatever. so in one loop i want it to check for multiple things instead of just "p12". does that tell you what you need? if not this may help, here is the whole function
    PHP Code:
    function p_search($t)
    {
        
    preg_match('/p([\d][\d]?)/',$t,$m);
        if (isset(
    $m[1]))
        {
            return (int)
    $m[1];
        } else {
            return 
    0;
        }


  • #4
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Untested, and may also match a dot after a p, but try:

    Code:
    preg_match_all('#[prz]\.?(\d{2})#', $t, $m);
    To match the p without a dot but the z or r with/without one:

    Code:
    preg_match_all('#(?:p|[rz]\.?)(\d{2})#', $t, $m);
    Last edited by MattF; 03-18-2010 at 01:11 AM.

  • #5
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Untested, and may also match a dot after a p, but try:

    Code:
    preg_match_all('/[prz]\.?(\d{2})/', $t, $m);
    that seems to work thanks. for future reference in case i decide to change "p", "r.", "z." to names such as "pat", "rob", and "zak" how would i go about doing that?

  • #6
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Code:
    preg_match_all('#(?:pat|rob|zak)(\d{2})#', $t, $m);

  • Users who have thanked MattF for this post:

    cosmicsea (03-18-2010)

  • #7
    Regular Coder
    Join Date
    Jan 2010
    Location
    Washington
    Posts
    223
    Thanks
    34
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by MattF View Post
    Code:
    preg_match_all('#(?:pat|rob|zak)(\d{2})#', $t, $m);
    Thanks alot matt!
    I appreciate the help you have gave to me multiple times now.


  •  

    Posting Permissions

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