This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
w3c markup validator ampersand (&) error
I am pulling records from a database to display but when I echo it W3C validator complains that there is & in the record.
I displays fine on the page. Is there something I can do to clean the string?
Presumably the error you are seeing is followed by this explanation:
An entity reference was found in the document, but there is no reference by that name defined. Often this is caused by misspelling the reference name, unencoded ampersands, or by leaving off the trailing semicolon (;). The most common cause of this error is unencoded ampersands in URLs as described by the WDG in "Ampersands in URLs".
Entity references start with an ampersand (&) and end with a semicolon (;). If you want to use a literal ampersand in your document you must encode it as "&" (even inside URLs!). Be careful to end entity references with a semicolon or your entity reference may get interpreted in connection with the following text. Also keep in mind that named entity references are case-sensitive; &Aelig; and æ are different characters.
Pay attention to that explanation and replace & (meaning "Start a character reference") with & (meaning "An ampersand character").
PHP has a function for converting all characters with special meaning in HTML into character references that you should use whenever you have some plain text that you want to put into an HTML document. Use it: htmlspecialchars() first.
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to print "that's it!" Exactly as same with double and single quotations in php? Can anyone have solution?
You just have to escape the single quote with a back slash
echo '"that\'s it!"';
I'll post something different because I like them and it think it's often missed or unknown by less experienced programmers.
HereDoc/NowDoc
//HereDoc works like " for variables
echo <<<TXT
"that's it!"
TXT;
The TXT tag can be anything that follows the same rules as variable names (\w+, and must start with Alpha), and the ending one must be on it's own line with "ABSOULTLY" nothing else besides ; (which can be omitted in arrays), nothing else not even whitespaces.
//NowDoc works like ' for variables
echo <<<'TXT'
"that's it!"
TXT;
It's often overlooked, as it's not documented in any really visible place, it's the third example on the string type page on PHP.net. Then after a bunch of scrolling you'll find the Nowdoc part, the only real difference is how they treat variables, as I mentioned.
http://php.net/language.types.string#language.types.string.syntax.heredoc
A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
Warning
It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS. The closing delimiter must also be followed by a newline.
If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.
This is also common in other languages besides PHP (with minor variations), and it's the cleanest way (for larger amounts of text, think HTML, JavaScript and a mix of those and PHP).
$selector = 'a.foobar';
echo <<<HTML
<script type="text/javascript">
;( function( $, window, document, undefined ) {
"use strict";
$(document).ready(function(){
$('tr.foobar').css('display', 'none');//singe quotes are fine
$("div.foobar").css("display", "none");//double quotes are too
$({$selector}).css('display', "none");//we can even mix them up if we want.
//we can use PHP variable like {$selector}, even these
//comments become comments in the JS.
//if that wasn't enough, most IDE's treat them like HTML
//so they are not greyed out, but nicely colored!
});
} ) ( jQuery, window, document );
</script>
HTML;
And this would work just fine, even the {$selector} would be replaced by PHP, the {} are optional except for method calls (unless they changed that). I put them in by habit because it colors them better in my IDE. Which is excatly how PHP treats variables in "normal" double quoted strings. (variable interpolation) except here we can use both types of quotes any way we want to...
If you do ever put one in an array it will only work this way (without the ;):
$a = [
<<<TXT
sometext
TXT
, "something else",
1,
2,
'etc..'
];
Other languages that use them (linked to the PHP section)
https://en.wikipedia.org/wiki/Here_document#PHP
In computing, a here document (here-document, here-text, heredoc, hereis, here-string or here-script) is a file literal or input stream literal: it is a section of a source code file that is treated as if it were a separate file. The term is also used for a form of multiline string literals that use similar syntax, preserving line breaks and other whitespace (including indentation) in the text.
The important thing is it does not use the quotes to define the string, so you are free to use them however you want, with no escaping.
One last thing I happened to notice from the PHP documentation that i never really read before.
the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including macOS
Maybe someone else knows, but I am not sure how important this bit really is. I program on a Windows Desktop \r\n, and then use the same exact files on a Linux server \n and Have Never 1 time had an issue with what that says. I do use editors though like Eclipse PDT, so it may default to the \n even on windows. But I have never had an issue on either one....
Enjoy!!
Probably one of the ways that works without escaping is as follows:
echo('"'."that'".'s it!"');
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 8 years ago.
I just heard of the PSR-2 coding standard in a comment on this question: Is there any reason to use the "public" keyword before method and member variable names?
I have a question on one of the rules in the PSR-2 standard:
The closing ?> tag MUST be omitted from files containing only PHP.
What is the point of that?
It is a good universal rule not using closing tag in php scripts. Note that everything after that closing tag is sent to client (browser) even white characters so if you're using closing tag and new line or any other white character it will be sent to browser. In most cases this behavior is not desirable.
To prevent issues from trailing whitespace http://hardcorewp.com/2013/always-omit-closing-php-tags-in-wordpress-plugins/
There where you read about PSR should be explanation like this: Because you may get problems with additional (unexpected) white spaces after the closing ?> tag - they will go to the output.
I just noticed that I could use an a variable as an argument, like this: $variable = "This's a string."; function('$variable'), and not like this: function('This's a string');. I can see why I can't do the latter, but I don't understand what's happening behind the scenes that meakes the first example work.
Have you heard about formal languages? The parser keeps track of the context, and so, it knows what the expected characters are and what not.
In the moment you close the already opened string, you're going back to the context before the opening of the string (that is, in the context of a function call in this case).
The relevant php-internal pieces of codes are:
the scanner turns the sequence between ' and ' into an indivisible TOKEN.
the parser puts the individual indivisible tokens into a semantic context.
These are the relevant chucks of C code that make it work. They are part of the inner workings of PHP (particularily, the Zend Engine).
PHP does not anticipate anything, it really reads everything char by char and it issues a parsing error as soon as it finds an unexpected TOKEN in a semantic context where it's not allowed to be.
In your case, it reads the token 'This' and the scanner matches a new string. Then it goes on reading s and when it finds a space, it turns the s into a constant. As the constant and the previously found token 'This' together don't form any known reduction (the possible reductions are described in the parser-link I've given you above), the parser issues an error like
Unexpected T_STRING
As you can deduce from this message, it is really referring to what it has found (or what it hopes it has found), so there's really no anticipation of anything.
Your question itself is wrong in the sense that there's no apostroph in the variable (in the variable's identifier). You may have an apostroph in the variable's value. Do not confuse them. A value can stand alone, without a variable:
<?php
'That\'s fine';
42;
(this is a valid PHP code which just loads those values into memory)
function('$variable') shouldn't be working correctly
Characters within the " " escape single quotes
Characters within '' do not escape single quotes (they cant escape themselves!).
Using the "" also lets you use variables as part of a string, so:
$pet = 'cat'
$myStory = "the $pet walked down the street"
function($pet) is the way the function should be passed a string
use it like this
function('This\'s a string');
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the use of <<<EOD in PHP?
I know how to use it, I know it's used to mark Heredoc's syntax to easily print multiple lines of string, but what is full name of that EOD acronym (if it's acronym) ?
Only thing I heard is End Of Data, but I'm not sure if it's good.
EOD isn't part of the Heredoc syntax. It's just used in their example.
$foo = <<<JAVASCRIPT
alert('Hello!');
alert('World!');
JAVASCRIPT;
This example would echo the javascript back to the user (or in other words, until the Token JAVASCRIPT is reached).
The heredoc identifier can be chosen at will:
http://php.net/manual/en/language.types.string.php
[T]he identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
So it might just mean "End Of Data" as well as "Explosive Ordnance Disposal", but it doesn't really matter, it's jsut an identifier, and it could as well have been _Fo0b4R as any other (valid) identifier.
I have a problem where I have some html like this
<p>There is the unfinished business of Taiwan, eventual “reunification”...a communiqué committing</p>
In that text string I would not want to change the < and > to & lt ; and ^ gt ;
However I would want to convert the quotes around “reunification” and the é in communiqué.
You will likely have to write your own htmlentities() replacement function. The easiest way would probably be to apply htmlentities(), and then replace < (or the numeric one, can't remember which php gives) with a <, and whatever other characters you want.
You might also be interested in Markdown, it is similar to what you are trying to accomplish, and might fit your needs.
http://daringfireball.net/projects/markdown/
http://michelf.com/projects/php-markdown/
'<' is a reserved character in XML. Section 2.3 of the XML standard strictly dictates that it MUST be escaped as either an entity or a character reference when used within character data. It is only allowed to appear in its unescapsed form when used as XML markup, or within a comment, processing instruction, or a CDATA section. Why do you want to bypass that requirement?