I am creating a pattern library similar to A List Apart's here: http://patterns.alistapart.com/. I am using the following code snippet to grab the contents of the HTML file and output it on the page (see the code area on A List Apart's example):
echo "<code class=\"col-md-8 col-sm-6 prettyprint pattern-markup language-markup\">".htmlspecialchars(#file_get_contents($dir.'/'.$ff))."</code>\n";
It is pulling in the code, but is stripping out all line breaks in the HTML so the code all runs together. Is it possible to add the line breaks back into the code view? I know nl2br is an option, but that works if I was outputting the HTML in visual mode, not code.
Thanks!
This has absolutely nothing to do with stripping newlines and everything to do with how basic HTML works.
Try this:
<p>Hello
World</p>
Do you see newlines? Nope.
Try applying the CSS white-space: pre-wrap to your element.
Just Use pre tags And everything will work fine. But put codes like this
<pre>Start from here
Not from here
</pre>
Related
Usually, I use nl2br() and it does come out just like it's entered in the textarea, but this causes a problem when using bbcode or posting code in <code> or <pre> tags, since it adds extra line breaks.
For example this code
[sub-title]test[/sub-title]
some text here.
I'd like it to look like that when displayed in the browser, but because [sub-title] becomes <div class="sub-title"> the <br /> adds an extra line break, so it will look like this (with 2 line breaks in between)
**test**
some text here.
I haven't fully looked into it yet, but could the PHP bbcode parser help, or is the only/best solution to use regex?
You can use nl2br()
Example
$message = nl2br(preg_replace('#(\\]{1})(\\s?)\\n#Usi', ']', stripslashes($message)));
I'm trying to put html into a mailto link (<a href='mailto:...) and using htmlescapechars() but this doesn't seem to be what I need.
<a href='mailto:?subject=<?echo $subject_e=htmlspecialchars($subject, ENT_QUOTES)?>&body=<?=$subject_e?>%20via%20https://mySite.com/<?=htmlspecialchars($subjectLink, ENT_QUOTES)?>%20'>some text</a>
Can anyone explain under what conditions Example #1 in the htmlspecialchars() part of PHP's documentation works?
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // <a href='test'>Test</a>
Running this function on my own server or on http://writecodeonline.com/php/ doesn't seem to convert the special characters.
So it doesn't appear to be a PHP version issue. Including the encoding parameter also had no effect. Wtf?
Running this function on my own server [...] doesn't seem to convert the special characters.
I bet you’re just thinking that because you view the script output as HTML … and what does < display as in HTML? Right, a < character …
You're looking at it wrong! The reason you're seeing the HTML tags IS because the magic php function is working. Otherwise, it would just render as actual HTML. This is happening because you're likely printing on to an HTML page.
So after looking elsewhere on SO, I realized instead of htmlspecialchars() I just needed rawurlencode().
It actually IS working. If you view the source, you will see it is working properly. The HTML is rendering the special chars though
If you look at the code of your page you'll see what was generated was exactly what was expected. Loo:
<div class="page-body">
<div id="code-output" class="code-output"><a href='test'>Test</a></div>
</div>
The problem is that when you render the page, the html entities are converted to text again. If it's not what you want, you should put the output inside <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.
I have a problem with the following:
I want to make a page that gets a file (I upload it), reads it and outputs it in an html file.
I am uploading the file and saving the contents in a mysql DB just fine, but when I show it again, I don't have any <br />'s there (maybe because the file should have \t\n or something.
How can I make it show it like it was originally written. (In the DB I see it with the fine spacing).
You probably want nl2br(). It will transform all line breaks to <br>s
You can either wrap inside <pre></pre> tags to display it as it is, or better yet use nl2br() function to add html break lines <br /> before any newline/carriage return /r /r/n /n
Are you sure the problem isn't just in the HTML? Multiple whitespaces convert to one in web browsers. In modern browsers, you can use the CSS white-space property to prevent that.
body { white-space: pre; }
Alternatively, you could wrap that section of HTML in a <pre> element, or you could hardcode extra spaces into
at time u store file data in database encode data using htmlentities() and at time of displaying decode it using html_entity_decode()
Let's say I have the following text file:
This is the first line of the text file.
This is the second line,
and here goes the third.
When using
echo file_get_contents($_SERVER{'DOCUMENT_ROOT'} . "/file.txt");
The output is
This is the first line of the text
file. This is the second line, and
here goes the third.
How do I prevent the layout from changing?
Thanks in advance :-)
In HTML, new line characters (\n or \r\n) don't cause actual line breaks to appear in the rendered page. Here are two possible solutions:
Use the nl2br() function to convert newlines to <BR> tags. This will work for some layouts, but not for ASCII art or others that rely on multiple spaces (which are reduced to one in HTML).
echo nl2br(file_get_contents(...));
Wrap the result in <pre> tags. This will keep all layout, but can look a bit ugly. You can style <pre> tags with CSS to make them prettier, if you'd prefer.
echo '<pre>' . file_get_contents(...) . '</pre>';
Every time you are gonna use PHP, it must be done in 2 parts:
Write your page in pure HTML. Make it work as desired. Ask HTML questions if any.
Write a PHP script which produce the the same text.
An HTML tag that could help you is <pre>
Another way to tell to browser that here hoes plain text is to send appropriate HTTP header
So, it this text is the only text being displayed on this page,
header("Content-type: text/plain");
readfile($_SERVER['DOCUMENT_ROOT'] . "/file.txt");