Jump to content

HTML Entities Numbers


WorldDrknss

Recommended Posts

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

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 &#8217;. 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.

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 &#8217;. 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.

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('"', '&#039', '<', '>');
$filtrate = preg_replace($search, $replace, $data);
$amp = preg_replace("/&[^#lgq]/", '& ', $filtrate);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.