getting pregmatch to ignore spaces

Discussion in 'PHP' started by mnymkr, Dec 16, 2008.

  1. #1
    I need pregmatch to ring true if my reg expression is: aloe vera

    and the string contains any combination of aloe vera: aloe vera, aloevera, aloe vera gel , aloe veragel

    i thought i could do this with the regex modifiers /ix but x is not working for me
     
    mnymkr, Dec 16, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
  3. atlantaazfinest

    atlantaazfinest Peon

    Messages:
    389
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    try using the g modifier
     
    atlantaazfinest, Dec 16, 2008 IP
  4. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #4
    yeah i tried that

    here are my results so far

    here is my string

    aloe vera aloe vera aloevera aloe vera juice

    if i use

    /aloe vera/gism

    match: aloe vera aloe vera aloevera aloe vera juice

    if I use /aloe vera/gixsm

    aloe vera aloe vera aloevera aloe vera juice

    i want one general expression that will capture all the aloe vera's with our without whitespace

    i mean think in the extreme, if somehow someone wrote alo e ver a i want to match it
     
    mnymkr, Dec 16, 2008 IP
  5. dprundle

    dprundle Peon

    Messages:
    399
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #5
    aloe[^\w]+vera
     
    dprundle, Dec 16, 2008 IP
  6. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #6
    May I ask why you're wanting this, is it to eliminate spam?
     
    Yesideez, Dec 16, 2008 IP
  7. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #7

    this doesn't work. it will only match aloe vera not aloevera
     
    mnymkr, Dec 16, 2008 IP
  8. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #8
    Why has this gone unnecessarily complex?

    $word = 'some aloeveragel and some aloe vera gel and some aloevera';
    preg_match_all('/aloe ?vera ?(gel)?/', $word, $matches);
    
    print_r($matches);
    PHP:
    Does the job fine... returns:

    Array
            (
                [0] => aloeveragel
                [1] => aloe vera gel
                [2] => aloevera
            )
    
    Code (markup):
     
    Danltn, Dec 16, 2008 IP
  9. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #9
    what do the ? marks mean
     
    mnymkr, Dec 16, 2008 IP
  10. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #10
    Optional...
     
    Danltn, Dec 16, 2008 IP
  11. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #11
    so basically you are just searching for aloe somewhere in the string?


    how is would this reply if you wanted to do: credit card AND creditcard but not credit by itself
     
    mnymkr, Dec 16, 2008 IP
  12. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #12
    <?php
    
    $word = 'This will find credit card and creditcard but not credit - how awesome!';
    preg_match_all('/credit ?card/', $word, $matches);
    
    print_r($matches);
    
    ?>
    PHP:
    Returns:
    
    Array
    (
        [0] => Array
            (
                [0] => credit card
                [1] => creditcard
            )
    
    )
    Code (markup):
     
    Danltn, Dec 16, 2008 IP
  13. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #13
    ok see that is what i don't get

    ? means optional

    so that reads to me

    credit and optional card

    so how does it know not to match just credit
     
    mnymkr, Dec 16, 2008 IP
  14. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #14
    No... 'credit' optional space, and 'card'.

    The ? operator only refers to the item directly before it... In this case - the space.
     
    Danltn, Dec 16, 2008 IP
  15. Yesideez

    Yesideez Peon

    Messages:
    196
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #15
    It's nice to be ignored... *sigh*
     
    Yesideez, Dec 16, 2008 IP
  16. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #16


    sorry

    i have a site that if you are an affiliate referral of mine, i convert those words to your affiliate id

    however there are plenty of tutorial on the net for converting links, i just coun't get the words to match like iw anted
     
    mnymkr, Dec 16, 2008 IP
  17. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #17

    Thank you

    I am curious, will preg match all work on an array or just a string
     
    mnymkr, Dec 16, 2008 IP
  18. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #18
    Danltn, Dec 16, 2008 IP
  19. herbacious

    herbacious Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    nice thread - cheers :)
     
    herbacious, Dec 19, 2008 IP