Advertisement
mpwalsh8

preg_match_all Server Crash

Mar 13th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2.     //  Instead of duplicating the FAQ content in the ReadMe.txt file,
  3.     //  let's simply extract it from the WordPress plugin repository!
  4.     //
  5.     //  This solutution is derived from a discussion found on www.DevNetwork.net.
  6.     //  See full discussion:  http://www.devnetwork.net/viewtopic.php?f=38&t=102670
  7.     //
  8.  
  9.     /*
  10.     //  Commented regex to extract contents from <div class="main">contents</div>
  11.     //  where "contents" may contain nested <div>s.
  12.     //  Regex uses PCRE's recursive (?1) sub expression syntax to recurs group 1
  13.  
  14.     $pattern_long = '{                    # recursive regex to capture contents of "main" DIV
  15.     <div\s+class="main"\s*>               # match the "main" class DIV opening tag
  16.       (                                   # capture "main" DIV contents into $1
  17.         (?:                               # non-cap group for nesting * quantifier
  18.           (?: (?!<div[^>]*>|</div>). )++  # possessively match all non-DIV tag chars
  19.         |                                 # or
  20.           <div[^>]*>(?1)</div>            # recursively match nested <div>xyz</div>
  21.         )*                                # loop however deep as necessary
  22.       )                                   # end group 1 capture
  23.     </div>                                # match the "main" class DIV closing tag
  24.     }six';  // single-line (dot matches all), ignore case and free spacing modes ON
  25.     */
  26.  
  27.     //  WordPress plugin repository places the content in DIV with a class of
  28.     //  "block-content" but there are actually several DIVs that have the same class.
  29.     //  We only want the first one.
  30.  
  31.     $url = 'http://wordpress.org/extend/plugins/wpgform/faq/' ;
  32.     $response= wp_remote_get($url) ;
  33.  
  34.     if (is_wp_error($response))
  35.     {
  36. ?>
  37. <div class="updated error"><?php _e('Unable to retrive FAQ content from WordPress plugin repository.', WPGFORM_I18N_DOMAIN);?></div>
  38. <?php
  39.     }
  40.     else
  41.     {
  42. ?>
  43. <?php
  44.         $data = &$response['body'] ;
  45.         $pattern_short = '{<div\s+[^>]*?class="block-content"[^>]*>((?:(?:(?!<div[^>]*>|</div>).)++|<div[^>]*>(?1)</div>)*)</div>}si';
  46.         $matchcount = preg_match_all($pattern_short, $data, $matches);
  47.         //$matchcount = 0 ;
  48.  
  49.         //  Did we find something?
  50.         if ($matchcount > 0)
  51.         {
  52.             //  The content we want will be the first match
  53.             echo($matches[1][0]); // print 1st capture group for match number i
  54.         }
  55.         else
  56.         {
  57. ?>
  58.     <div class="updated error"><?php _e('Unable to retrive FAQ content from WordPress plugin repository.', WPGFORM_I18N_DOMAIN);?></div>
  59. <?php
  60.         }
  61.     }
  62.        
  63. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement