How to retrieve code from database with php ? - php

I want to store HTML code in mysql database. If I want to store the following code into database
<html>
<body>
<p> this is a paragraph </p
</body>
</html>
they store as they are. But when I retrieve them and echo with php the tag get vanished. But I want to echo them as they are above. I also want to store and show not only HTML but other code (c,java,php) also. Anyone have any idea?

you can use htmlentities () php function to echo html codes
$str = "
<html>
<body>
<p> this is a paragraph </p
</body>
</html>
";
echo htmlentities($str);
You can also use htmlspecialchars();

You can use htmlentities($str) for that, another nice thing to use is <pre></pre>
Putting those tags around the code will preserve newlines, tabs and spaces. In case you want to showcase it.

Related

Php echo html. Use newline, \n, or not?

Is there any difference on this:
echo "</body>\n</html>";
and
echo "</body></html>";
I wonder if the 'newline' has any affect on browser.
Yes, \n does have effect on the output of your HTML. As you can see Here.
echo "</body>\n</html>";
outputs your HTML as:
</body>
</html>
while
echo "</body></html>";
outputs your HTML as
</body></html>
The "\n" doesn't have any effect on the displayed html on the browser. If you want to go to the next line, echo a <br>
The
echo "</body>\n</html>";
have no echo in browser but you can see th effect in source code (the \n is not an html element and is not processed by browser
so you can see an effect in the code generated page (crtl+U) as
</body>
</html>
if you need an effect in browser you should use a proper tag as <br />
echo "</body><br /></html>"; // in this case poorly useful do the fact add a new line between a not (normally useful part
There is no difference in your case. However, newline leads to anonymous text block insertion in case it is between two inline (or inline-block) elements:
<h2>Newline</h2>
<p>
<span>first</span>
<span>second</span>
</p>
<h2>No newline</h2>
<p>
<span>first</span><span>second</span>
</p>
<h2>newline in comment</h2>
<p>
<span>first</span><!--
--><span>second</span>
</p>
There're some techniques to avoid these blocks, like inserting comments or setting font-size: 0 for container element.
Lotta details might be found in the article.

Script for Code tag from php/html/css

I wrote some code from my project to textarea and save it to my database.
after i wanted read and write from database to some panel on web and the code did not have the spacing and line spacing
example:
i write to textare this
function write($text) {
echo"$text";
}
and this code looks like this panel
function write($text) {
echo"$text";
}
please thank you for replies
and sorry for my english :D thanks
Try wrapping your text area with <pre> tags as found in this answer here:
https://stackoverflow.com/a/19313432/2677083
Sample code from #DanFromGermany:
Using <pre>:
<html>
<body>
<pre class="yourStyleForThisPreFormattedText">
Welcome <?php $a = $_POST["input"];
echo $a; ?>
</pre>
</body>
</html>
Explanation from #DanFromGermany:
This is because new line characters are represented as \r\n, in the
sourcecode you'll see new lines. Whitespaces get truncated if one
follows another in HTML.
I suggest you to use the tag, which does not only save the new
lines (like php's nl2br()) but also preserves the whitespaces.
Notice:
For HTML4 and HTML5 use nl2br($str, true);, for XHTML4 use
nl2br($str); - the difference is in the output: <br> and <br />. See http://php.net/nl2br

Replace a string with HTML

I'm using str_replace as follows:
<html>
<head>
</head>
<body>
<script type="text/php">
$new_str = str_replace( '[[str_to_replace]]' , $GLOBALS['html'] , $original_str );
</script>
<div class="wrapper">[[str_to_replace]]</div>
<?php
// multiple includes
// lots and lots of code
//PHP code to calculate HTML code
//value of $html depends on data calculated after div.wrapper is drawn
$GLOBALS['html'] = '<input type="text" />';
?>
</body>
</html>
I'm forced to wrap the PHP code in a script tag because the document is getting passed to a library as an HTML document. The library has the ability to execute PHP code inside script tags but in this case is working oddly.
What I'm expecting:
[[str_to_replace]] should become an HTML input field.
What I'm getting:
[[str_to_replace]] becomes the literal string <input type="text" />.
How do I get the second result?
You're likely misinterpreting wht you can do with inline script. In dompdf HTML is parsed separately from inline script. Any HTML you insert into the document using inline script will be treated as plain text. What you should be doing is parsing your document first then passing the results to dompdf.
FYI, It's hard to see from your sample exactly what you're doing in the code. Plus we can't see what's going on with dompdf. I'm having a hard time seeing how everything ties together.
It sounds like what you're trying to do is to replace the string with decoded HTML entities. You'll probably want to do:
$htmlEntityString = '&'; // String containing HTML entities that you want to decode.
$new_str = str_replace( '[[str_to_replace]]' , html_entity_decode($htmlEntityString) , $original_str );
In this case, whatever HTML you have with HTML entity form will be decoded and will replace the substring.
Read more about it for all the options:
http://php.net/manual/en/function.html-entity-decode.php

Prevent php from rendering/executing between pre tags

I'm using pre tags to display codesnippets on my website, these snippets contain html and php.
So far I've used Jquery to convert html in the codesnippets to plain text, but the php tags are still being executed.
The code that I used for converting the html contents of the pre tag to plain text:
$(document).ready(function(){
$("pre").text($("pre").html());
});
An example of what I'm using it on:
<pre>
<p>paragraph Content</p>
<h1>html code</h1>
<?php echo "this is php example code"; ?>
</pre>
The first 2 lines display in the browser with the tags and everything perfectly, but the third line execute the php tags rendering only this php example code. Can anyone help me out so I can display the php tags in my code snippet aswell?
Also if anybody knows a better solution to rendering html/css and php as a code snippet on your website It would be really helpfull.
The problem with your approach is, that you try to influence the rendering after the browser has processed the file. When you look at the sourcecode of your page, you will notice that there really is nothing else than "this php example code" in there, because the server parsed that part of your code and executed it, which leads to nothing but the string you put into that echo command.
You need to handle your output before your send it to the client and fetch all occurrences of <pre> and </pre> to replace all < and > between them with their respective HTML entities.
Have a look at this question to find out the best approach to parse the HTML and get the appropriate elements in order to modify them.
You should escape the < and > symbols - replace them with their HTML entities, < and > respectively ;)
If you want a total and comprehensive conversion to HTML entities, try converting them over at http://centricle.com/tools/html-entities
Generally it's good practice to convert all symbols to their respective HTML entities within a code block.
Try to replace <?php ... ?> with
<? .... >
<pre>
<p>paragraph Content</p>
<h1>html code</h1>
<?php echo "this is php example code"; ?>
</pre>
Here's a JSFiddle example so you can see how it renders on the page.

Get text with PHP Simple HTML DOM Parser

i'm using PHP Simple HTML DOM Parser to get text from a webpage.
The page i need to manipulate is something like:
<html>
<head>
<title>title</title>
<body>
<div id="content">
<h1>HELLO</h1>
Hello, world!
</div>
</body>
</html>
I need to get the h1 element and the text that has no tags.
to get the h1 i use this code:
$html = file_get_html("remote_page.html");
foreach($html->find('#content') as $text){
echo "H1: ".$text->find('h1', 0)->plaintext;
}
But the other text?
I also tried this into the foreach but i get the full text:
$text->plaintext;
but it returned also the H1 tag...
It looks like $text->find('text',2); gets what you're looking for, however I'm not sure how well that will work when the amount of text nodes is unknown. I'll keep looking.
You can simply strip html tags using strip_tags
<?php
strip_tags($input, '<br>');
?>
Use strip tags, as #Peachy pointed out. However, passing it a second argument <br> means string will ignore <br> tags, which is unnecessary. In your case,
<?php
strip_tags($text);
?>
would work as you'd like, given that you are only selecting content in the content id.
Try it
echo "H1: ".$text->find('h1', 0)->innertext;

Categories