Advertisement
jargon

Linedancer :: "regex/scriptMatches.php"

Sep 10th, 2024 (edited)
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.93 KB | Gaming | 0 0
  1. <?php
  2. // Shared :: "regex/scriptMatches.php"
  3.  
  4. class scriptMatchClass {
  5.  
  6.     public $regex = [];
  7.  
  8.     public function __construct(){
  9.         $file = file_get_contents("{$_SERVER['DOCUMENT_ROOT']}/Shared/scripts/regex/scriptMatches.csv");
  10.         $lines = explode("\r\n", $file);
  11.  
  12.         $parts = [];
  13.  
  14.         foreach ($lines as $line) {
  15.             $key = substr($line, 0, strpos($line, "="));
  16.             $value = substr($line, strpos($line, "=") + strlen("="));
  17.             $parts[$key] = $value;
  18.         }
  19.  
  20.         // Escape 'before' and 'after' parts using '/' as the delimiter
  21.         foreach (['before', 'after'] as $key) {
  22.             $parts[$key] = preg_quote($parts[$key], '/'); // Use '/' as the delimiter here
  23.         }
  24.  
  25.         $this->regex = [];
  26.         foreach ($parts as $key => $value) {
  27.             if (!in_array($key, ['before', 'after'])) {
  28.                 // Escape the pattern using '/' as the delimiter
  29.                 $escapedPattern = preg_quote($parts[$key], '/');
  30.                 $this->regex[$key] = '/' . $parts['before'] . $escapedPattern . $parts['after'] . '/mi';
  31.             }
  32.         }
  33.     }
  34.  
  35.     public function asName($line) {
  36.         // Apply the regex for both 'full' and 'short' patterns
  37.         $fullMatches = preg_match_all($this->regex['full'], $line, $matchesFull, PREG_SET_ORDER);
  38.         $shortMatches = preg_match_all($this->regex['short'], $line, $matchesShort, PREG_SET_ORDER);
  39.  
  40.         if ($fullMatches > 0) {
  41.             $matches = $matchesFull[0];
  42.         } elseif ($shortMatches > 0) {
  43.             $matches = $matchesShort[0];
  44.         } else {
  45.             return [null, null];
  46.         }
  47.  
  48.         $fullFile = isset($matches['full']) ? $matches['full'] : '';
  49.         $shortFile = isset($matches['short']) ? $matches['short'] : '';
  50.        
  51.         return [$fullFile, $shortFile];
  52.     }
  53.  
  54.     public function jRmNumeric(&$m = false) {
  55.         if (!is_array($m)) {
  56.             return;
  57.         }
  58.  
  59.         foreach ($m as $key => $value) {
  60.             if ($key == 0) {
  61.                 continue;
  62.             }
  63.             if ($key == strval(intval($key))) {
  64.                 unset($m[$key]);
  65.             } else {
  66.                 if (is_numeric($value)) {
  67.                     $m[$key] = intval($m[$key]);
  68.                 }
  69.             }
  70.         }
  71.         return $m;
  72.     }
  73. }
  74.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement