1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

regular expressions, preg_match

Discussion in 'PHP' started by Erind, May 11, 2010.

  1. #1
    I have a few variabes, which are usually abbreviated. Here's an example.

    $a="Inter M";
    $b="FC Internazionale Milano";

    Obviously, $a != $b.

    Suppose [-] means anystring. We do:

    $a=str_replace(" ", "[-]", $a); // Inter[-]M
    $a="[-]". $a ."[-]"; // [-]Inter[-]M[-]

    Then $a does equal $b, because [-] fills in the rest of the missing components.

    Can someone tell me how this is done in regular expressions, because the above is obviously not php syntax but rather something that i wrote to demonstrate.

    Thx, Erind
     
    Erind, May 11, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    Ellaborate?, do you mean this:

    <?php
    $a = "Inter M";
    $b = "FC Internazionale Milano";
    
    $a = preg_quote($a);
    $a = str_replace(" ", ".+?", $a); 
    
    if (preg_match('~'.$a.'~i', $b)){
    echo "{$a} == {$b}";
    }
    
    ?>
    PHP:
     
    Last edited: May 11, 2010
    danx10, May 11, 2010 IP
  3. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #3
    if(preg_match('/.*Inter.+M.*/i', $b)) {
        echo 'Matched!';
    }
    PHP:
     
    JAY6390, May 11, 2010 IP
  4. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Heres a bit more info.

    The abbreviated version of the variable is always found in the full version. The abbreviated version usually misses parts or has shortened versions. Here are some examples that would be matched.

    $a=$b
    Inter= FC Internazionale Milano
    Inter M= FC Internazionale Milano
    Man Utd=Manchester Utd
    Bayern=Bayern Munchen
    G. Bistra=Gloria Bistra

    As you can see the example I said works in every case. Just the last case, G. Bistra, has a period and space as a separator.

    Bottom Line: Get rid of irregular characters and spaces, and match them with anything as long as the rest matches.

    Thx
     
    Erind, May 11, 2010 IP
  5. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Are there a number of different matches or will the short version always be the same?
     
    JAY6390, May 11, 2010 IP
  6. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #6
    There are different matches, but the idea/format is always right, like I demonstrated. Match alphanumerical values, and guess the rest. 99.9% it will be correct. That .1 percent I will take care of manually, lol. Dan was on the right track just needs slight tweeking.
     
    Erind, May 11, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    Was it you who hired me a while back regarding soccer xml? - if so come online so we can talk..
     
    danx10, May 11, 2010 IP
  8. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Yessir :)

    I can't come online. I am at work.

    Here's what's up: The match is recorderd, everything is great. I have another xml source that checks for wins/losses. This source houwever uses the full names of each team instead. So sometimes they match, sometimes they dont. When they don't its the case like I said where I have an abbreviated version of the team name.

    BTW, that script turned out to be more than 100 php files, and 4 js files on top of the css/html. I thought it was easy to start with...
     
    Erind, May 11, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    You could try the folllowing (but this would match if any occurence within $a is within $b):

    if (stristr($b, $a)){
    //matched..
    }
    PHP:
    Perhaps Jay could give a better response as im not a regex guru.

    and regarding the 100 php files lol - I believe i only gave you 1 php file (which was around 30 lines using DOM), not sure why your having to have 100 files - its what happens when you use php uneffectively.
     
    danx10, May 11, 2010 IP
  10. Erind

    Erind Peon

    Messages:
    663
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Well, what happened was I thought I needed to read xml and then process it. well not so much. read xml, select data using ajax, store data using php, check data, confirm data against xml again, user system, admin panel, omggggg. I have literally been working on it since back when was it, february? not consistantly but at least 5+ hours a week
    
    if (stristr($b, $a)){
    //matched..
    }
    
    PHP:
    That would match, Inter in Internazionale, but not Man Utd in Manchester Utd...
     
    Erind, May 11, 2010 IP
  11. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #11
    
    $a="Inter M";
    $b="FC Internazionale Milano";
    
    // Remove all the non-alphabetic/space characters
    $stripped = preg_replace('/[^a-z\s]+/i', '', $a);
    
    //Split the string into single words
    $words = explode(' ', $stripped);
    
    //Add .+ between words and .* either side of them and create a regex
    $regex = '/.*'.implode('.+', $words).'.*/i';
    
    //Test regex
    if(preg_match($regex, $b)) {
        echo 'MATCH';
    }else{
        echo 'NO MATCH';
    }
    
    PHP:
    Try that
     
    JAY6390, May 11, 2010 IP