11 / 07 / 2009

parse sınıfı

ekleyen: Emre Çevik kategori: codeigniter| php| proje

simplexml ile ilgili daha önce yazı yazmıştım. xml ve html yi parse edecek bir sınıf yazacağız.

bu sınıfı daha sonra geliştirip projemizde kullanacağız.

sınıfımız ne yapacak.

xml dosyasından xpath ile title, link ve description alacak.
html sayfasından title, description ve resimleri çekecek.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
 
class Parse {
 
    var $xpath;
    var $xml;
    var $html;
 
    function load_html($url) {
         $this->html = file_get_contents($url);
    }
 
    function load_file($url) {
        $this->xml = simplexml_load_file($url, 'SimpleXMLElement', LIBXML_NOCDATA);
    }
 
    function load_string($string) {
        $this->xml = simplexml_load_string($string, 'SimpleXMLElement', LIBXML_NOCDATA);
    }
 
    function xpath($key, $path) {
        $this->xpath[$key] = $this->xml->xpath($path);
    }
 
    function get_array() {
        $array = array();
 
        foreach($this->xpath as $key => $attribute)
        {
            $i=0;
            foreach($attribute as $element) {
 
                $result = (string) $element;
                $array[$i][$key] = $this->clear($result);
                $i++;
            }
        }
        return $array;
    }
 
    function preg_match_all($regex) {
        preg_match_all($regex,$this->html,$patterns);
 
        $patterns = array_unique($patterns[1]);
        $array = array();
 
        foreach($patterns as $pt)
        {
            $array[] = $this->clear($pt);
        }
        return $array;
    }
 
    function preg_match($regex) {
        preg_match($regex,$this->html,$patterns);
        return $this->clear($patterns[1]);
    }
 
    function clear($string) {
        return htmlspecialchars($string);
    }
 
 
}
?>

sınıfımızın kullanımı

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Ornek extends Controller {
 
	function Ornek()
	{
		parent::Controller();	
	}
	function parse()
	{
            $this->load->library('parse');
 
            $this->parse->load_file('http://code.internet.com.tr/feed/');
 
            $this->parse->xpath("title","/rss/channel/item/title");
            $this->parse->xpath("link","/rss/channel/item/link");
 
            $array = $this->parse->get_array();
 
            $this->parse->load_html('http://code.internet.com.tr/simplexml');
            $baslik = $this->parse->preg_match('#<title>(.*?)<\/title>#si');
            $description = $this->parse->preg_match('(meta\sname=\"description\"\scontent=\"(.*)\"[ /]*>)');
            $images = $this->parse->preg_match_all("/img[ ]+src=[\"' ]?([^\"' >]+)[\"' ]?[^>]*>/i");
 
            echo '<pre>';
            echo $baslik.'<br>';
            echo $description.'<br>';
            print_r($images).'<br>';
            print_r($array).'<br>';
 
	}
}

http://localhost/project/index.php/ornek/parse adresine girip ekran çıktısını kontrol edebilirsiniz.

  • Digg
  • del.icio.us
  • Facebook
  • Google
  • BlinkList
  • blogmarks
  • description
  • Furl
  • Ma.gnolia
  • NewsVine
  • Reddit
  • Technorati

benzer konular:

  1. simplexml
  2. auth sınıfı kullanımı
  3. auth sınıfı
  4. flyweight design pattern – php
  5. smarty
  6. builder design pattern – php
  7. composite design pattern – php
  8. 3 katmanlı mimari ve php -2-
  9. codeigniter kurulumu
  10. polymorphism


(1 votes, average: 5.00 out of 5)
Loading ... Loading ...

1 yorum

1 | Hacı Osman

01 / 08 / 2009 - 06:44

Avatar

Güzel olmuş elinize sağlık.

yorum yaz

ne yapıyoruz

eğleniyoruz?

anket

design pattern yazılarını nasıl buldunuz?

sonuçlar

Loading ... Loading ...