SimplePie_Misc::uncomment_rfc822 PHP Method

uncomment_rfc822() public method

Remove RFC822 comments
public uncomment_rfc822 ( $string ) : string
return string Comment stripped string
    function uncomment_rfc822($string)
    {
        $string = (string) $string;
        $position = 0;
        $length = strlen($string);
        $depth = 0;
        $output = '';
        while ($position < $length && ($pos = strpos($string, '(', $position)) !== false) {
            $output .= substr($string, $position, $pos - $position);
            $position = $pos + 1;
            if ($string[$pos - 1] !== '\\') {
                $depth++;
                while ($depth && $position < $length) {
                    $position += strcspn($string, '()', $position);
                    if ($string[$position - 1] === '\\') {
                        $position++;
                        continue;
                    } elseif (isset($string[$position])) {
                        switch ($string[$position]) {
                            case '(':
                                $depth++;
                                break;
                            case ')':
                                $depth--;
                                break;
                        }
                        $position++;
                    } else {
                        break;
                    }
                }
            } else {
                $output .= '(';
            }
        }
        $output .= substr($string, $position);
        return $output;
    }

Usage Example

Exemplo n.º 1
0
 function parse_date($dt, $rfc822_tz = true)
 {
     static $cache = array();
     if (!isset($cache[$dt][$rfc822_tz])) {
         $dt = SimplePie_Misc::uncomment_rfc822($dt);
         /*
         Capturing subpatterns:
         1: RFC 822 date
         2: RFC 822 day
         3: RFC 822 month
         4: RFC 822 year
         5: ISO 8601 date
         6: ISO 8601 century
         7: ISO 8601 year
         8: ISO 8601 month
         9: ISO 8601 day
         10: ISO 8601 ordinal day
         11: ISO 8601 month
         12: ISO 8601 day
         13: ISO 8601 week
         14: ISO 8601 day of week
         15: Time
         16: Hour
         17: Hour Decimal
         18: Minute
         19: Minute Decimal
         20: Second
         21: Second Decimal
         22: Timezone
         23: Diff ±
         24: Hour
         25: Hour Decimal
         26: Minute
         27: Minute Decimal
         28: Alphabetic Timezone
         */
         if (preg_match('/^(?:(?:(?:Mon|Tue|Wed|Thu|Fri|Sat|Sun)[,\\s]+)?(([0-9]{1,2})\\s*(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\\s*([0-9]{4}|[0-9]{2}))|(([0-9]{2})(?:([0-9]{2})(?:(?:-|\\s)*(?:([0-9]{2})([0-9]{2})|([0-9]{3})|([0-9]{2})(?:(?:-|\\s)*([0-9]{2}))?|W([0-9]{2})(?:(?:-|\\s)*([0-9]))?))?)?))((?:T|\\s)+([0-9]{2})(?:(?:,|\\.)([0-9]*)|(?:\\:|\\s)*([0-9]{2})(?:(?:,|\\.)([0-9]*)|(?:\\:|\\s)*([0-9]{2})(?:(?:,|\\.)([0-9]*))?)?)?(?:\\s)*((?:(\\+|-)([0-9]{2})(?:(?:,|\\.)([0-9]*)|(?:\\:|\\s)*(?:([0-9]{2})(?:(?:,|\\.)([0-9]*))?))?)|(UTC|GMT|EST|CST|MST|PST|EDT|CDT|MDT|PDT|UT|[A-IK-Z]))?)?$/i', $dt, $match)) {
             // Fill all matches
             for ($i = count($match); $i <= 28; $i++) {
                 $match[$i] = '';
             }
             // Set blank vars
             $year = 1970;
             $month = 1;
             $day = 1;
             $hour = 0;
             $minute = 0;
             $second = 0;
             $timezone = false;
             // RFC 822
             if ($match[1] !== '') {
                 if (strlen($match[4]) == 2) {
                     $year = $match[4] < 70 ? "20{$match['4']}" : "19{$match['4']}";
                 } else {
                     $year = $match[4];
                 }
                 switch (strtolower($match[3])) {
                     case 'jan':
                         $month = 1;
                         break;
                     case 'feb':
                         $month = 2;
                         break;
                     case 'mar':
                         $month = 3;
                         break;
                     case 'apr':
                         $month = 4;
                         break;
                     case 'may':
                         $month = 5;
                         break;
                     case 'jun':
                         $month = 6;
                         break;
                     case 'jul':
                         $month = 7;
                         break;
                     case 'aug':
                         $month = 8;
                         break;
                     case 'sep':
                         $month = 9;
                         break;
                     case 'oct':
                         $month = 10;
                         break;
                     case 'nov':
                         $month = 11;
                         break;
                     case 'dec':
                         $month = 12;
                         break;
                 }
                 $day = $match[2];
             } else {
                 // Year
                 if ($match[7] !== '') {
                     $year = "{$match['6']}{$match['7']}";
                     // Two Digit Month/Day
                     if ($match[11] !== '') {
                         $month = $match[11];
                         if ($match[12] !== '') {
                             $day = $match[12];
                         }
                     } elseif ($match[8] !== '') {
                         $month = $match[8];
                         $day = $match[9];
                     } elseif ($match[10] !== '') {
                         $day = $match[10];
                     } elseif ($match[13] !== '') {
                         // Week Day
                         if ($match[14] !== '') {
                             $day = $match[14];
                         }
                         $first_day_of_year = date('w', mktime(0, 0, 0, 1, 1, $year));
                         if ($first_day_of_year == 0) {
                             $first_day_of_year = 7;
                         }
                         $day = 7 * ($match[13] - 1) + $day - ($first_day_of_year - 1);
                     }
                 } else {
                     $year = "{$match['6']}00";
                 }
             }
             // Time
             if ($match[15] !== '') {
                 $time = 0;
                 $time += ($match[16] + ('.' . $match[17])) * 3600;
                 $time += ($match[18] + ('.' . $match[19])) * 60;
                 $time += $match[20] + ('.' . $match[21]);
                 $hour = floor($time / 3600);
                 $time -= $hour * 3600;
                 $minute = floor($time / 60);
                 $time -= $minute * 60;
                 $second = round($time);
                 // Timezone
                 if ($match[22] !== '') {
                     // Alphabetic Timezone
                     if ($match[28] !== '') {
                         // Military
                         if (strlen($match[28]) == 1) {
                             if ($match[28] == 'Z' || $match[28] == 'z' || !$rfc822_tz) {
                                 $timezone = 0;
                             } else {
                                 $timezone = ord(strtoupper($match[28]));
                                 if ($timezone > 74) {
                                     $timezone--;
                                 }
                                 if ($timezone <= 76) {
                                     $timezone = -($timezone - 64);
                                 } else {
                                     $timezone -= 76;
                                 }
                                 $timezone *= 3600;
                             }
                         } else {
                             switch (strtoupper($match[28])) {
                                 case 'UT':
                                 case 'UTC':
                                 case 'GMT':
                                     $timezone = 0;
                                     break;
                                 case 'EST':
                                     $timezone = -18000;
                                     break;
                                 case 'CST':
                                     $timezone = -21600;
                                     break;
                                 case 'MST':
                                     $timezone = -25200;
                                     break;
                                 case 'PST':
                                     $timezone = -28800;
                                     break;
                                 case 'EDT':
                                     $timezone = -14400;
                                     break;
                                 case 'CDT':
                                     $timezone = -18000;
                                     break;
                                 case 'MDT':
                                     $timezone = -21600;
                                     break;
                                 case 'PDT':
                                     $timezone = -25200;
                                     break;
                             }
                         }
                     } else {
                         $timezone = 0;
                         $timezone += ($match[24] + ('.' . $match[25])) * 3600;
                         $timezone += ($match[26] + ('.' . $match[27])) * 60;
                         $timezone = (int) round($timezone);
                         if ($match[23] == '-') {
                             $timezone = -$timezone;
                         }
                     }
                 }
             }
             if ($timezone === false) {
                 $cache[$dt][$rfc822_tz] = mktime($hour, $minute, $second, $month, $day, $year);
             } else {
                 $cache[$dt][$rfc822_tz] = gmmktime($hour, $minute, $second, $month, $day, $year) - $timezone;
             }
         } elseif (($time = strtotime($dt)) > 0) {
             $cache[$dt][$rfc822_tz] = $time;
         } else {
             $cache[$dt][$rfc822_tz] = false;
         }
     }
     return $cache[$dt][$rfc822_tz];
 }