[php+rss] <content:encoded> parsen

Pagina: 1
Acties:
  • 25 views sinds 30-01-2008

  • ilovetechno
  • Registratie: Oktober 2001
  • Laatst online: 24-11 02:36
Ik heb een script om bepaalde info uit een RSS feeds naar MySQL te schrijven. Nu lukt het me iet om de <content:encoded> te parser.

Het script:
PHP:
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

$connection = mysql_connect("x",
                            "x_x",
                            "x");
mysql_select_db("x", $connection);

$counter = 0;
$type = 0;
$tag = "";
$itemInfo = array();
$channelInfo = array();
function unhtmlspecialchars( $string )
    {
        $string = str_replace ( '&amp;', '&', $string );
        $string = str_replace ( ''', '\'', $string );
        $string = str_replace ( '&quot;', '\"', $string );
        $string = str_replace ( '&lt;', '<', $string );
        $string = str_replace ( '&gt;', '>', $string );
        
        return $string;
    }

function opening_element($xmlParser, $name, $attribute){


global $tag, $type;

$tag = $name;

if($name == "CHANNEL"){
$type = 1;
}
else if($name == "ITEM"){
$type = 2;
}

}//end opening element

function closing_element($xmlParser, $name){

global $tag, $type, $counter;

$tag = "";
if($name == "ITEM"){
$type = 0;
$counter++;
}
else if($name == "CHANNEL"){
$type = 0;
}
}//end closing_element

function c_data($xmlParser, $data){

global $tag, $type, $channelInfo, $itemInfo, $counter;

$data = trim(htmlspecialchars($data));

if($tag == "TITLE" || $tag == "DESCRIPTION" || $tag == "THUMB" || $tag == "LINK"){
if($type == 1){

$channelInfo[strtolower($tag)] = $data;

}//end checking channel
else if($type == 2){

$itemInfo[$counter][strtolower($tag)] .= $data;

}//end checking for item
}//end checking tag
}//end cdata funct

$xmlParser = xml_parser_create();

xml_parser_set_option($xmlParser, XML_OPTION_CASE_FOLDING, TRUE);
xml_parser_set_option($xmlParser, XML_OPTION_SKIP_WHITE, TRUE);

xml_set_element_handler($xmlParser, "opening_element", "closing_element");
xml_set_character_data_handler($xmlParser, "c_data");

$fp = file($_GET['rss']);

foreach($fp as $line){
if(!xml_parse($xmlParser, $line)){
die("Could not parse file.");
}
}

foreach($itemInfo as $items){
    $query = mysql_query("SELECT * FROM stories WHERE title = '".htmlentities($items['title'],
          ENT_QUOTES)."'") or die(mysql_error());
    $num = mysql_num_rows($query);
    if($num > 0){
        echo $items['title']." already exists!<br />";
    }
    else {
        $items2['description'] = unhtmlspecialchars ($items['description']);
        

        if (mysql_query("INSERT INTO stories VALUES('', '".htmlentities($items['title'], 
                 ENT_QUOTES)."', '".htmlentities($items2['description'], ENT_QUOTES)."', 
                  '".htmlentities($items['link'],ENT_QUOTES)."')") or die(mysql_error())){
        echo $items['title']." was added!<br />";
        }
    }
}

?>


Is er iemand die mij een beetje op weg kan helpen ? :)

  • RobIII
  • Registratie: December 2001
  • Niet online

RobIII

Admin Devschuur®

^ Romeinse Ⅲ ja!

(overleden)
Het is in PRG niet de bedoeling dat je een topicstart maakt van 2 regels en dan 100+ regels code dumpt; we verwachten van jezelf ook wat inzet zoals je dat kunt lezen in onze Programming Beleid Quickstart. Daarnaast is dit gewoon een quickfix en daar doen we hier niet aan.

Je bent van harte welkom met een nieuw topic, maar hou dan de quickstart in acht.

There are only two hard problems in distributed systems: 2. Exactly-once delivery 1. Guaranteed order of messages 2. Exactly-once delivery.

Je eigen tweaker.me redirect

Over mij


Dit topic is gesloten.