Code in a RSS feed - php

I am using a feed creator (specifically, Kohana's feed::create()), except some of my text might be like this in the description element
See code below
<?php echo 'example'; ?>
The feed creator is using the SimpleXML Library. Whenever the data is returned (using $xml->asXml()) the html angle brackets inside the description element are converted to HTML entities.
This makes the tags be parsed correctly, useful for p tags and the like. However, in this case - the PHP code won't show up (being surrounded by angle brackets).
My question is - how can I show stuff like this in a RSS feed? How can I display > when it itself is parsed back as <? Does that make sense?
Here is an example of what is being outputted:
<description><p>some content</p>
<p>WITH some code</p><p><?php
//test me out!
?></p>
</description>
(note that is not an error above - the entities are all converted)
What I'd like it to display (in a RSS reader) is
some content
WITH some code
<?php
//test me out! ?>

You want the code to actually display in the feed as code, not execute, right? If so, you need to escape it the same way you would if you wanted it to display in HTML, i.e.:
htmlspecialchars( "<?php echo 'example'; ?>" )
That will result in your feed looking even more garbled than it already does, because the PHP will be double-encoded, once for the RSS XML and again for the HTML contained in the RSS XML.

All RSS tags contain strings so can't you just do your PHP manipulation prior to setting the tag?
So instead of saying:
$xml->description = 'Description <?php echo $var; ?>';
you should be doing:
$xml->description = 'Description ' . $var;
What is the reason that you want to pass PHP code into your RSS feed? I'm guessing that a lot of feed readers would not execute it anyways.

Related

Removing Paragraph Tags from get_the_excerpt($variable)

Is there any way to remove the paragraph marks (probably created by some tags wrapping the get_the_excerpt() method in wordpress)??
I am currently using: $product_description = get_the_excerpt($product_id);
I wanted to simply echo <?php echo(get_the_excerpt()); ?> but it gives me a parsing error because I have to enter it in a variable.
=> I guess we can't echo when we define a variable.
I am trying to retrieve my data from my product description but this wrapping kill my html tags. It's obvious but should looks like this:
Any idea? Thanks!

HTML doesn't get rendered, why does it happen?

On a PHP+MySQL project, there's a string of text coming from a MySQL table that contains HTML tags but those tags never get rendered by Google Chrome or any browser I tried yet:
You can see that the HTML (p, strong) aren't getting interpreted by the browser.
So the result is:
EDIT: HTML/PHP
<div class="winery_description">
<?php echo $this->winery['description']; ?>
</div>
$this->winery being the array result of the SQL Select.
EDIT 2: I'm the dumbest man in the world, the source contains entities. So the new question is: How do I force entities to be interpreted?
Real source:
Any suggestions? Thanks!
You are probably using innerText or textContent to set the content of your div, which just replace the child nodes of the div with a single text node.
Use innerHTML instead, to have the browser parse the HTML and create the appropriate DOM nodes.
The answer provided by #Paulpro is correct.
Also note that if you are using jQuery, be sure to use the .html() method instead of .text() method:
$('#your_element').html('<h1>This works!</h1>');
$('#another_element').text('<h2>Wrong; you will see the <h2> in the output');

Use PHP to echo whats inside div tags

I dont know what to research or where to start here.
What im trying to do is use PHP to read an HTML Page and pull out the raw text contained inside a div
the div is this
<div class="thingy">
test
</div>
When the php is executed, I want it to echo
Test
Is there an easy snippet for this, or can someone post a small script?
Edit: the html page with the Div is on another webpage.
What you're looking to do is parse HTML. Use the DOM module that comes with PHP to do this: http://php.net/manual/en/book.dom.php
You do NOT want to try to do this with regular expressions.
If you want to remove ALL the HTML tags from a document, use the PHP strip_tags() function: http://us3.php.net/strip_tags
While this could possibly be done using regex, I would recommend using a DOM parser. My reccommendation goes to SimpleHTML Dom Parser. Using it, here's how you would do what you want
$string = "<div class=\"thingy\">test</div>";
$html = str_get_html($string); // create the DOM object
$div = $html->find('div[class=thingy]', 0); // find the first div with a class of 'thingy'
echo $div->plaintext(); // echo the text contents
If you want to parse your html you can use it like
<?php
$str = '<div class="thingy">test</div>';
echo strip_tags($str);//OUTPUT : test
?>
As your html is on other webpage, start output buffering include that file in your main php script, do all manipulation on it to get the content.

formalize xml input to simpleXML

I write a simple function to create xml element with data like :
$item->addChild('title', '<![CDATA[<font color="#9353ce">All Content Is Dynamically Loaded</font>]]>');
But the data won't output like I want but it contains some html entities :
<![CDATA[<font color="#FFFFFF">
I've checked the function html_entity_decode but it seems to convert double quotes and single quotes only :)
Thanks for reading this and I hope someone can help me .
You need to use htmlspecialchars-decode() to get them back to the format you are looking for.
In XML,
<title>
<![CDATA[<font color="#9353ce">All Content Is Dynamically Loaded</font>]]>
</title>
is semantically the same as
<title>
<font color="#9353ce">All Content Is Dynamically Loaded</font>
</title>
So perhaps the following could have the desired output (the second version above)?
$item->addChild(
'title',
'<font color="#9353ce">All Content Is Dynamically Loaded</font>'
);
(unless you have other reasons why you really need to serialize as a CDATA section)

htmlspecialchars and json encode problem

I am trying to format some bad html to output into a pop window. The html is stored in a field in a mysql database.
I have been performing json_encode and htmlspecialchars on the row in the php like so:
$html = htmlentities(json_encode($row2['ARTICLE_DESC']));
and calling my makewindows function, which simply takes the html as a paramter and uses it withdocument.write like so:
<p>Click for full description </p>
This works ok, as in some html code is produced, such as the following:
http://www.nomorepasting.com/getpaste.php?pasteid=22823&seen=true&wrap=on&langoverride=html4strict
pasted there because I do not know how to wrap lines in SO
The problem is that htmlspecialchars does not seem to be stripping bad html data, as no popup window is created. The error I receive with firebug is
missing ) after argument list
However the html is outside of my control.
From what I have read, I am taking the correct steps. If I am missing something out, what is it?
My full make windows function:
function makewindows(html){
child1 = window.open ("about:blank");
child1.document.write(html);
child1.document.close();
}
You shouldn't have the single quotes in the function call. It should look like this:
<p>Click for full description </p>
Then the output will look like
<p>Click for full description </p>
which is correct.
Try it the following way:
$html = htmlentities(json_encode($row2['ARTICLE_DESC']),ENT_QUOTES);
I think the single quotation marks are not escaped by default.
Nevertheless I recommend you saving the html in a JavaScript variable before opening the window.

Categories