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)
Related
Is the current PHPExcel can now format HTML tags inside excel cell?
This is like question here
I have a table from database that has field that contains string with html tags
i.e. < b > hello < / b >
and I want it to output in excel not as a plain text but something like this hello
Is there any php to excel library that can do this? any idea? thanks in advance
No! PHPExcel doesn't have any built-in logic to do this; and nor does any other library that I'm aware of.
You'd need to write some code yourself to handle the conversion from HTML to a Rich Text Run.... there's some logic inside the HTML Reader that you might be able to use as the basis for this.
EDIT
Since this answer was written, a helper class has been added to the PHPExcel library that will take a basic block of simple html markup and convert it to a rich text object that can be set as a cell value. This is the PHPExcel_Helper_HTML class, with it's toRichTextObject() method, that takes an argument of a block of html and returns a Rich Text Object. There are examples demonstrating its use in Examples/42richText.php
$html = '<font color="#0000ff">
<h1 align="center">My very first example of rich text<br />generated from html markup</h1>
<p>
<font size="14" COLOR="rgb(0,255,128)">
<b>This block</b> contains an <i>italicized</i> word;
while this block uses an <u>underline</u>.
</font>
</p>
<p align="right"><font size="9" color="red">
I want to eat <ins><del>healthy food</del> <strong>pizza</strong></ins>.
</font>
';
$wizard = new PHPExcel_Helper_HTML;
$richText = $wizard->toRichTextObject($html);
While not all markup is supported, and it doesn't use stylesheets, and only a limited set of inline style elements, it works well enough with basic markup elements.
I have spent a lot of time on this thing. But i am not getting goal. Thier is a no way write like this and get data as our wish. I apperitiate with Mark answer, Try with your own script.
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');
i use dom in php to retrieve a div's content by $node->nodeValue. This div has many <br/> tags in its content, but after i store it in the database and output it in the browser, all the <br/> tags are changed to the whitespace. I want to keep the <br/> tags, how do i achieve that?
nodeValue returns only the text-data (if used on element-nodes). Retrieve the contents using saveXML()
$node->ownerDocument->saveXML($node);
DOMNode::nodeValue will only return the text content.
As <br /> is a child element, it won't be returned.
Your best bet is to
Create an empty, temporary string
Loop over all the child nodes in your $node
Get the markup of each child node using DOMDocument::saveHTML()
Concatenate this string with your temp one
Save the temp string to the database
Something like this - http://www.php.net/manual/en/book.dom.php#89718
Assuming you are using MySQL (since you don't say) make sure you use the function mysql_real_escape_string. Dr.Molle's answer might provide further insight.
http://php.net/manual/en/function.mysql-real-escape-string.php
I am having a textbox, in that i have loaded a xml file.
After editing and saving the xml content into the xml file, the content is not in the right formate.
While loading again, its not in the xml format
How to save a the content into the file with neat alignment?
Please help me
For ExampleI need to save like the following
<section>
<value>a</value>
<value>b</value>
</section>
But after saving its looks like
<section><value>a</value><value>b</value></section>
Thanks,Praveen J
As Gordon says your question makes no sense - the XML fragment is still "well-formed" (but its far from complete) so it is in the right format.
Do you mean you want to preserve the format it was submitted in? In which case output it using <pre>...</pre> tags. OTOH there are standard tools out there which wil format XML according to specific standards - e.g. geshi
C.
I think is issue is that it doesn't preserve whitespace, so opening the xml file later shows it all in a single line as opposed to spaced/tabbed as originally created.
You can try white-space: physical as a CSS attribute on your textarea. Alternatively you can try adding the attribute/value pair "wrap=hard" to your textarea declaration. Both methods should preserve whtepace.
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.