WorldDrknss Posted August 16, 2008 Share Posted August 16, 2008 I am completely unsure how to proceed with what I want to achieve. I have a script that will call RSS feeds and post them to the database. All the information gets passed into filters such as htmlentities which is giving some unpleasant results later on. The problem is that RSS feeds use html entities numbers such as ’ and after it as passed through htmlentities is gets parsed out as ’ and using html_entitity_decode beats the purpose of using htmlentities. I am not exactly sure on how to proceed here. What I need it to do is read the rss feed, post into the database while at the same time being able to print out the proper characters and being secure. Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/ Share on other sites More sharing options...
papaface Posted August 16, 2008 Share Posted August 16, 2008 No idea what you are trying to achieve here. If you want to display the real character use html_entitity_decode as you suggested. <?php echo html_entity_decode("’"); ?> Not entirely sure what your problem is. Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-617917 Share on other sites More sharing options...
thebadbad Posted August 16, 2008 Share Posted August 16, 2008 Before running htmlentities(), you could search for HTML entities and replace the ampersand with something unique (that's not translated with htmlentities()), run htmlentities(), and then replace the unique strings with ampersands again. <?php $string = 'test © 2008 ’. This ampersand should indeed become an entity: &. Some HTML: <strong>bold</strong>.'; $string = preg_replace('~&([#0-9a-z]+~i', ';^^;$1', $string); $string = htmlentities($string); $string = str_replace(';^^;', '&', $string); echo $string; ?> As long as the unique string (;^^ isn't part of the feed string, it works as intended. Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-617939 Share on other sites More sharing options...
thebadbad Posted August 16, 2008 Share Posted August 16, 2008 The forum messed up the ’ entity in the $string. &#8217; should be ’ Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-617940 Share on other sites More sharing options...
WorldDrknss Posted August 16, 2008 Author Share Posted August 16, 2008 Before running htmlentities(), you could search for HTML entities and replace the ampersand with something unique (that's not translated with htmlentities()), run htmlentities(), and then replace the unique strings with ampersands again. <?php $string = 'test © 2008 ’. This ampersand should indeed become an entity: &. Some HTML: <strong>bold</strong>.'; $string = preg_replace('~&([#0-9a-z]+~i', ';^^;$1', $string); $string = htmlentities($string); $string = str_replace(';^^;', '&', $string); echo $string; ?> As long as the unique string (;^^ isn't part of the feed string, it works as intended. Thanks I will give this a shot tonight. Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-618120 Share on other sites More sharing options...
WorldDrknss Posted August 16, 2008 Author Share Posted August 16, 2008 I did this which seems to work very well. It works just like htmlspecialchars except that it will only convert lone & There are disadvantages to this, like anything with &l &g &q will not convert and some another html strings starting with & $search = array('/\"/', '/\'/', '/</', '/>/'); $replace = array('"', ''', '<', '>'); $filtrate = preg_replace($search, $replace, $data); $amp = preg_replace("/&[^#lgq]/", '& ', $filtrate); Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-618212 Share on other sites More sharing options...
WorldDrknss Posted August 19, 2008 Author Share Posted August 19, 2008 in the end I found a simple way. $replace = preg_replace("/\&#([0-9]{2,4});/", "&#$1;", $string); Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-619692 Share on other sites More sharing options...
thebadbad Posted August 19, 2008 Share Posted August 19, 2008 Except that will only work for entity numbers, not names. Like © and . But if the feeds only use entity numbers, that's fine Link to comment https://forums.phpfreaks.com/topic/119946-html-entities-numbers/#findComment-620004 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.