[PHP] Nederlands characters van een rss feed parsen*

Pagina: 1
Acties:
  • 131 views sinds 30-01-2008
  • Reageer

Onderwerpen


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Edit: met PHP trouwens

Goede middag, sorry als mijn Nedelands een beetje aan de slechte kant is maar.....

.... ik ben de hele week bezig on de "special characters" uit een Nederlands RSS te "filteren", met veel "hacks", ben ik daar, bijna.

De feed is http://feeds.feedburner.com/EhealthMedicalfactsnl

Volgens mb_detect_encoding() is de feed encoded in ASCII, dus ik heb
code:
1
header('Content-type: text/html; charset=ASCII');
toegevoegd.

Ik heb ook
code:
1
2
3
printf('<a href="main.php?a=2">%s</a><br />',
                        htmlspecialchars($this->title,ENT_QUOTES,'UTF-8'));
                    }

gebruikt in de class.

Daarna begon ik handmatig alle "raar" characters uit te vissen
code:
1
$this->title = str_replace ( '&euml;', 'ë', $this->title );


Zover was ik, dan kwam een
code:
1
$
in de text, dus ik heb
code:
1
$this->title = str_replace ( '$', '?', $this->title );
in de code toegevoegd....................... maar zo blijf ik oneindige bezig

Ik hoop dat iemand een oplossing heeft die veel korter is te omschrijven dan dit post was.

Thanks in advance,

- Richard

[ Voor 12% gewijzigd door Verwijderd op 13-08-2005 13:29 ]


Acties:
  • 0 Henk 'm!

Verwijderd

De beste manier is om gewoon XML te gaan gebruiken als XML, en niet als een tekstbestand. Lees dus met speciale functies het XML document, en analyseer het vervolgens.

Wil je toch met eenvoudige string functies iets bereiken, kijk dan op php.net naar de reacties bij de functie htmlentities.

Acties:
  • 0 Henk 'm!

  • Michali
  • Registratie: Juli 2002
  • Laatst online: 29-05 22:54
Je zou preg_replace kunnen gebruiken:
PHP:
1
$this->title = preg_replace("/&#?[0-9a-z]{2,4};/", "?", $this->title);

Of iets dergelijks.

Maar ik zou toch eerst proberen wat Cheatah voorstelt.

Noushka's Magnificent Dream | Unity


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Verwijderd schreef op zaterdag 13 augustus 2005 @ 13:34:
Wil je toch met eenvoudige string functies iets bereiken, kijk dan op php.net naar de reacties bij de functie htmlentities.
Bedankt voor de snel reactie, en ja ik kende al de reacties van de php.net link, maar er zit nog een week werken om ze helemaal door te nemen, denk ik.
De beste manier is om gewoon XML te gaan gebruiken als XML, en niet als een tekstbestand. Lees dus met speciale functies het XML document, en analyseer het vervolgens.
Dit snap ik niet, helemaal.

Ik wil de feed publiceren op een web-pagina, dus het moet toch uiteindelijk geparsd van xml naar een textbestand, of mis ik iets eenvoudig hier?

Acties:
  • 0 Henk 'm!

  • Michali
  • Registratie: Juli 2002
  • Laatst online: 29-05 22:54
Maar hoe parse je het nu dan? Je kunt wel gaan klooien met allerlei string functie en regexen terwijl het ook 'simpel' kan met de DOM extensie bijvoorbeeld. Dan hoef je je ook niet druk te maken om die entities.

Noushka's Magnificent Dream | Unity


Acties:
  • 0 Henk 'm!

  • PrisonerOfPain
  • Registratie: Januari 2003
  • Laatst online: 26-05 17:08
Kun je ipv die str_replace's niet beter kijken html_entities_decode, of de functie voor lagere versies die in die manual entry staat?

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Met code rechtstreeks uit de "Php Cookbook" ( = out of my depth )

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
// rss classes

class pc_RSS_item{
    
    var $title = '';
    var $description = '';
    var $link = '';
  var $pubDate = '';
  
    function display(){
        //echo mb_detect_encoding('this->description');
        $this->description = str_replace ( '&amp;', '&', $this->description );
        $this->description = str_replace ( 'ë', 'ë', $this->description ); 
        $this->description = str_replace ( '&euml;', 'ë', $this->description );
        $this->description = str_replace ( '&rsquo;', '\'', $this->description );
        $this->description = str_replace ( '&lsquo;', '\'', $this->description );
        $this->description = str_replace ( '[image1_left]', '', $this->description );
        $this->description = str_replace ( 'Â&#8217;', '\'', $this->description );
        $this->description = str_replace ( 'Â&#8216;', '\'', $this->description );
        $this->description = str_replace ( '&euro;', '€', $this->description );
        $this->description = str_replace ( '&iuml;', 'ï', $this->description );
        $this->description = str_replace ( '&eacute;', 'é', $this->description );
        //$this->description = str_replace ( 'i?n', 'ë', $this->description );
        $this->description = str_replace ( '&egrave;', 'è', $this->description );
        $this->description = str_replace ( '&nbsp;', '', $this->description );
        //$this->description = str_replace ( '$', '$', $this->description ); // wont read the '€'  
        //$this->title = str_replace ( '$', '$', $this->title );
        $this->title = str_replace ( '&nbsp;', '', $this->title );
        $this->title = str_replace ( '&egrave;', 'è', $this->title );
        //$this->title = str_replace ( 'i?n', 'ë', $this->title );
        $this->title = str_replace ( '&eacute;', 'é', $this->title );
        $this->title = str_replace ( '&amp;', '&', $this->title );
        $this->title = str_replace ( '&euml;', 'ë', $this->title );
        $this->title = str_replace ( 'ë', 'ë', $this->title ); 
        $this->title = str_replace ( 'Â&#8217;', '\'', $this->title );
        $this->title = str_replace ( 'Â&#8216;', '\'', $this->title );
        $this->title = str_replace ( '&lsquo;', '\'', $this->title );       
        $this->title = str_replace ( '&rsquo;', '\'', $this->title );
        $this->title = str_replace ( '[image1_left]', '', $this->title );
        $this->title = str_replace ( '&euro;', '€', $this->title );
        $this->title = str_replace ( '&iuml;', 'ï', $this->title );
        $this->pubDate = substr_replace ( $this->pubDate, '',0,4);
        $this->pubDate = substr_replace ( $this->pubDate, '',12,18);
        printf('<a href="main.php?a=2">%s</a><br />',
                        htmlspecialchars($this->title,ENT_QUOTES,'UTF-8'));
                    }
}

class pc_RSS_parser{
    var $tag;
    var $item;
    
    function start_element($parser, $tag, $attributes){
            if('item' == $tag){
                $this->item = new pc_RSS_item;
            }elseif(!empty($this->item)){
                $this->tag = $tag;
        }
    }
    function end_element($parser, $tag){
        if('item' == $tag){
            $this->item->display();
            unset($this->item);
        }
}
        function character_data($parser, $data){
        if(!empty($this->item)){
            if(isset($this->item->{$this->tag})){
                $this->item->{$this->tag} .= trim($data);
            }
        }
    }
}

    //rss gedoe
        $xml = xml_parser_create();
        $rss = new pc_RSS_parser_newspage;
        xml_set_object($xml, $rss);
        xml_set_element_handler($xml, 'start_element', 'end_element');
        xml_set_character_data_handler($xml, 'character_data');
        xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, false);
        //$feed = 'http://nu.nl/deeplink_rss2/index.jsp?r=Algemeen';
        $feed = 'http://feeds.feedburner.com/medicalfacts';
        $fp = fopen($feed, 'r') or die("Not getting the rss feed");
        while($data = fread($fp, 4096)){  //4096
            xml_parse($xml, $data, feof($fp)) or die("Parse ain't working today, sir");
        }
        fclose($fp);
        xml_parser_free($xml);

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
PrisonerOfPain schreef op zaterdag 13 augustus 2005 @ 13:52:
Kun je ipv die str_replace's niet beter kijken html_entities_decode, of de functie voor lagere versies die in die manual entry staat?
Good call, maar met

PHP:
1
html_entity_decode($this->description,ENT_QUOTES,'ISO8859-15'));


krijgt ik

code:
1
 patiënt


in de text Firefox

of
code:
1
patiC+nt


Win IE6 :(

[ Voor 9% gewijzigd door Verwijderd op 13-08-2005 14:07 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Voor de duidelijkheid hier is de aangepaaste functie...

PHP:
1
2
3
4
5
    function display(){
        printf('<a href="main.php?a=2">%s</a><br />',
                        html_entity_decode($this->title,ENT_QUOTES,'ISO8859-15'));
                    }
}

Acties:
  • 0 Henk 'm!

  • PrisonerOfPain
  • Registratie: Januari 2003
  • Laatst online: 26-05 17:08
Het is dan ook ISO-8859-15, niet ISO8859-15. Daarbij is het ook de bedoeling dat je die charset ook meestuurt naar de browser.

Acties:
  • 0 Henk 'm!

  • Michali
  • Registratie: Juli 2002
  • Laatst online: 29-05 22:54
PrisonerOfPain schreef op zaterdag 13 augustus 2005 @ 16:15:
Het is dan ook ISO-8859-15, niet ISO8859-15. Daarbij is het ook de bedoeling dat je die charset ook meestuurt naar de browser.
ISO8859-15 is een alias, dus dat zou niet zo veel uit moeten maken.

Noushka's Magnificent Dream | Unity


Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
PrisonerOfPain schreef op zaterdag 13 augustus 2005 @ 16:15:
Het is dan ook ISO-8859-15, niet ISO8859-15. Daarbij is het ook de bedoeling dat je die charset ook meestuurt naar de browser.
Aangepaste, geen verandering, maar als ik de charset ook in de header gooi...

code:
1
header('Content-type: text/html; charset=ISO-8859-15');


Krijg ik....

code:
1
patiënt


in plaats van "patiënt" in IE en Firefox, wat is tenminste en stap dichter bij.

[ Voor 10% gewijzigd door Verwijderd op 13-08-2005 16:41 ]


Acties:
  • 0 Henk 'm!

Verwijderd

Dat ziet er uit als een UTF-8 string die wordt gelezen alsof het een ISO-8859-1 string is. Zorg eens dat het script charset=utf-8 in de content-type header meestuurt, of gebruik eens utf8_decode om er een iso-8859-1 string van te maken?

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Verwijderd schreef op zaterdag 13 augustus 2005 @ 16:42:
Dat ziet er uit als een UTF-8 string die wordt gelezen alsof het een ISO-8859-1 string is. Zorg eens dat het script charset=utf-8 in de content-type header meestuurt, of gebruik eens utf8_decode om er een iso-8859-1 string van te maken?
Dat is hem !!!!!!!

Bedankt Cheatah, en iedereen voor uw snel en zeer helpzalm antwoorden.

Acties:
  • 0 Henk 'm!

  • PrisonerOfPain
  • Registratie: Januari 2003
  • Laatst online: 26-05 17:08
Michali schreef op zaterdag 13 augustus 2005 @ 16:37:
ISO8859-15 is een alias, dus dat zou niet zo veel uit moeten maken.
Overlooked those! Damnit;

Acties:
  • 0 Henk 'm!

  • Skaah
  • Registratie: Juni 2001
  • Laatst online: 16-09 18:38
Je kan ook een RSS-bouwer gebruiken, bijvoorbeeld Feedcreator.

Acties:
  • 0 Henk 'm!

Verwijderd

Topicstarter
Skaah schreef op zaterdag 13 augustus 2005 @ 19:57:
Je kan ook een RSS-bouwer gebruiken, bijvoorbeeld Feedcreator.
Er, nee (Edit: denk ik), ik wil de feed op een web pagina zetten.

SimpleXml (php5) ziet mooie uit voor mijn toepassing trouwens.

[ Voor 3% gewijzigd door Verwijderd op 13-08-2005 20:20 ]

Pagina: 1