WXR_Parser_Regex::parse PHP Метод

parse() публичный Метод

public parse ( $file )
    function parse($file)
    {
        $wxr_version = $in_post = false;
        $fp = $this->fopen($file, 'r');
        if ($fp) {
            while (!$this->feof($fp)) {
                $importline = rtrim($this->fgets($fp));
                if (!$wxr_version && preg_match('|<wp:wxr_version>(\\d+\\.\\d+)</wp:wxr_version>|', $importline, $version)) {
                    $wxr_version = $version[1];
                }
                if (false !== strpos($importline, '<wp:base_site_url>')) {
                    preg_match('|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url);
                    $this->base_url = $url[1];
                    continue;
                }
                if (false !== strpos($importline, '<wp:category>')) {
                    preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category);
                    $this->categories[] = $this->process_category($category[1]);
                    continue;
                }
                if (false !== strpos($importline, '<wp:tag>')) {
                    preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag);
                    $this->tags[] = $this->process_tag($tag[1]);
                    continue;
                }
                if (false !== strpos($importline, '<wp:term>')) {
                    preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term);
                    $this->terms[] = $this->process_term($term[1]);
                    continue;
                }
                if (false !== strpos($importline, '<wp:author>')) {
                    preg_match('|<wp:author>(.*?)</wp:author>|is', $importline, $author);
                    $a = $this->process_author($author[1]);
                    $this->authors[$a['author_login']] = $a;
                    continue;
                }
                if (false !== strpos($importline, '<item>')) {
                    $post = '';
                    $in_post = true;
                    continue;
                }
                if (false !== strpos($importline, '</item>')) {
                    $in_post = false;
                    $this->posts[] = $this->process_post($post);
                    continue;
                }
                if ($in_post) {
                    $post .= $importline . "\n";
                }
            }
            $this->fclose($fp);
        }
        if (!$wxr_version) {
            return new WP_Error('WXR_parse_error', __('This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer'));
        }
        return array('authors' => $this->authors, 'posts' => $this->posts, 'categories' => $this->categories, 'tags' => $this->tags, 'terms' => $this->terms, 'base_url' => $this->base_url, 'version' => $wxr_version);
    }

Usage Example

Пример #1
0
 function parse($file)
 {
     // Attempt to use proper XML parsers first
     if (extension_loaded('simplexml')) {
         $parser = new WXR_Parser_SimpleXML();
         $result = $parser->parse($file);
         // If SimpleXML succeeds or this is an invalid WXR file then return the results
         return $result;
     } else {
         if (extension_loaded('xml')) {
             $parser = new WXR_Parser_XML();
             $result = $parser->parse($file);
             // If XMLParser succeeds or this is an invalid WXR file then return the results
             return $result;
         }
     }
     // use regular expressions if nothing else available or this is bad XML
     $parser = new WXR_Parser_Regex();
     return $parser->parse($file);
 }
All Usage Examples Of WXR_Parser_Regex::parse