htmlentities in PHP - differences between local and live - php

I edit certain pages of my website via textarea's and a common WYSIWYG editor. This editing frequently requires me to display HTML code by using <pre> tags.
Due to the nature of the textarea, I believe I'm required to use htmlentities in order to conserve HTML Entities such as < (which is translated to <), so here is what I did:
<textarea name="resume" class="ckeditor"><?php echo htmlentities($e['resume']) ?></textarea>
This works great locally, my syntax highlighter works great in the pre tags, the textarea doesn't convert the HTML Entities when being edited.
So I try hosting this project, but on live there is a problem, all double quotes are being displayed as \" , there is a slash before all double quotes in my text.
Is this something I should take up with my host? I don't understand how locally this issue isn't happening.

You have Magic Quotes enabled on the live site.
Look at the documentation for magic_quotes_gpc: http://php.net/manual/en/info.configuration.php#ini.magic-quotes-gpc
You can take it up with your host, but you can also disable it in a .htaccess file: http://php.net/manual/en/security.magicquotes.disabling.php

Related

A small issue with PhP on Mac's text editor

I am editing my PhP files with Text Edit on my Mac.
The PhP files are source code I got from some tutorial.
The subtle issue I was able to notice is that the apostrophe ' on the source file is not the same apostrophe I type with my keyboard.
Also, when I try to type inside an existing string with the "source apostrophe" I notice the last letter goes outside the string and I am sure this is not a typo from my side.
When I put my keyboard apostrophe in the PhP file I get a 500 internal server error when requesting the PhP file.
If I just copy paste a "source apostrophe" I don't get the internal error.
Any idea what is going on here?
EDIT: As funny as it may be, I have put the 3 apostrophe here. The first one is from the source code(most left) the other two are from my keyboard.
' ' `
I just had a similar problem and discovered it was the Mac text editor automatically substituting Smart Quotes for single quotes. In TextEdit this can be disabled for the current document by unchecking the menu item Edit->Substitutions->Smart Quotes. Or uncheck Smart Quotes in the TextEdit Preferences->New Document->Options for all new documents.
Ok, I figured this out.
The apostrophe I was getting from my keyboard in Text Edit was UTF 8, it was 3 bytes long.
The "good" apostrophe was simply 27 hex.
I am now editing my PhP files with xCode instead of Text Edit.
I am guessing text edit does some rich text editing? Not sure.
This problem is related to MacOS System.
Solution:
System Preferences -> Keyboard -> Text Tab
for singled quotes: 'abc'
for doubled quotes: "abcd"
there, you can change the selected value.

PHPStorm: PHP Syntax Highlighting editing / adding rules, and Customization

Hey I was looking around online and couldn't find anything about how to modify the syntax highlighting rules. I am trying to add a rule for PHP variables inside of double quoted strings. In Aptana they have a rule where these variables are highlighted which I find extremely useful as I do a lot with PHP vars inside double quoted strings. This is similar to the <<
I would also like to be able to specify sets of rules, for instance on heredocs:
<<<HTML
<html>
<div>$phpvar</div>
</html>
HTML
I want The html highlighted as HTML for specifically <<
Anyone know how to customized / edit the rules for the syntax highlighting?
Go into settings
File > Default Settings
There you can select how your code is formatted wrapped etc.
For colors go to:
Preferences > Editor (under IDE settings) > Colors & Fonts
There you can play around with colors, highlighting, background, fonts etc

What would cause html code formatting to disappear in php page?

I have a page written with php where, for some reason, all of the plain html content of the file index.php goes on one line (look at the source) The white space is preserved, but all the new-lines disappear.
I cannot come up with any reason why this would happen, short of a syntax error, but I went through with a fine toothed comb, and found nothing out of place. This only happens on the index.php page.
Anyone have any Ideas what I should be looking for? I can post more code if necessary.
<?php
//...
include('ssi/header.php');
?>
<div>
<section id="charters">
<h2>Tanker Chartering</h2>
<!-- ... -->
The above code evaluates to something like this:
<div> <section id="charters"> <h2><a href="charters.php">Tanker ...
Maybe you have linux server and you're using windows system. Different operating systems use different new line characters. Also, for one server my FTP client uploaded it with wrong formatting, and missed every line break.
Also applications like
Notepad++ gives you the ability to change formatting and linebreaks.
It's probably the encoding of the file combined with the transfer mode on the ftp from which you downloaded/uploaded the file. Try using something like notepad2, and saving the file in UTF-8 rather than ANSI. Also upload/download with your FTP program in binary not ASCII. That stopped all of my newline issues with PHP.
could it be that your hosting provider is doing some kind of minimization for you? no newlines means less characters pused down the wire.

How to make a textarea ouput textarea code without breaking?

I created a form where users can enter html code and it outputs their code in another textarea. The problem is that if the html the user enters has a textarea in the code, the in their code breaks my textarea form. I see other sites display any html correctly so how is this done without breaking the form and allowing the user to copy it so that it still remains as and not some converted code so they can paste it on their webpage?
Ah crap yeah I figured it out, in fact the problem wasn't with the htmlspecialchars code alone I forgot to add a return to one of my functions haha. Thanks guys.
Represent characters that have special meaning in HTML using entities. Since you are using PHP, use htmlspecialchars
There are millions and millions of ways to do this. The easiest is to use htmlspecialchars or htmlentities on the user's input. This will make a visual </textarea> in the textarea box without closing it. This actually turns it into </textarea>. htmlspecialchars transforms less characters than htmlentities and usually makes more sense to use in a situation like this, but do your research.
strip_tags() is also a possibility.
You can also use a regular expression with PCRE, or even str_replace() or other string manipulation functions to strip off the textarea, convert the special characters, etc.
PECL also as a BB code extension you can use if you still want your users to be able to enter some for of tags to style their output.
<textarea><?php echo htmlentities($code); ?></textarea>
You have to transform the html code into symbols, so it is not treated as html.
Use the function htmlentities() on the textarea content before echoing it.

Have I misunderstood what heredoc should do?

I'm very new to PHP so I know I am missing something obvious here -
I thought the heredoc function is supposed to retain formatting, line breaks, etc.
But whenever I test it, while it parses, there is no formatting.
I've tried lots of different scripts including copy-and-pasts from sources such as PHP.net and W3schools - so I know there is nothing wrong with the scripts. Can't google up an answer to this one - probably because it's too obvious?? (BTW, testing with MAMP). Thanks.
HEREDOC is not a function, it is a method for specifying string delimiters that allows you to forgo escaping quotes within the heredoc (I like it because good text editors will syntax highlight their contents based on the heredoc name).
HEREDOCs do preserve all whitespace, newlines, etc. I think you are probably looking at the results in a browser. Browsers generally trim all whitespace, newlines, spaces, and tabs included, down to a single space. Try looking at it on the command line, a text email, or a text file.
It will produce a string identical to the one you set.
However, browsers render multiple whitespace condensed to one space character. This is by design.
To preserve your spaces, you can use the pre element (assuming default browser stylesheet) or white-space: pre CSS property.
<div style="white-space: pre">
<?php echo <<<A
some text
preserved
just
how
you want it.
A; ?>
</div>
jsFiddle.
Because you said "testing with MAMP" I assume, that you use your webbrowser to display the content. Thats a slightly wrong approach, because the webbrowser itself strips down any unnecessary whitespaces. Look at the source of the page you display in your browser and you will see the "real" content.
It does, but only for what it outputs.
If it outputs HTML, then that HTML is going to be interpreted by a browser using the normal rules for handling whitespace.

Categories