SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2005
    Posts
    41
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help witjh Preg_Match_All with Regex

    Hi guys,

    Being plagued with annoying spam over the last few weeks on my forums and looking to build a quick function to detect strings that look like this:

    Watch (*) Online
    Stream (*) Online

    So if the post string = $post_text - are there any code examples as to how I could use preg_match_all to detect it - if $post_text contains at least one instance of the above, then do this.

    Many thanks,
    Jorge

  2. #2
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Not tested
    PHP Code:
    <?php
    preg_match_all
    ('#[watch,stream](.+)online#'strtolower($str), $match);
    var_dump$match );
    ?>
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel

  3. #3
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vectorialpx View Post
    Not tested
    PHP Code:
    <?php
    preg_match_all
    ('#[watch,stream](.+)online#'strtolower($str), $match);
    var_dump$match );
    ?>
    The square bracket group means any character within the bracket selection so you are basically eliminating any words containing those letters and a comma!

    This should work better and preg_match (without all) should be suffictient:

    PHP Code:
    $match_exists preg_match('#(watch|stream).+?online#i'$str); 
    This will match these strings:

    buy watch online now!
    watch online
    Watches online
    WATCH-ONLINE
    WATCH any text here provided it's in one line(!) ONLINE
    Stream online
    best streaming online

    If you want to also catch cases when the two words are in different lines then add the 's' modifier to the end of the regex.

  4. #4
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    The square bracket group means any character within the bracket selection so you are basically eliminating any words containing those letters and a comma!
    Ouch! I messed up the parentheses types (must confess I'm not a regExp expert).
    Be nice to nerds. Chances are you'll end up working for one - Bill Gates
    > photos | admin panel


Tags for this Thread

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
  •