I am having trouble with this piece of code below. Whenever I run this file from browser it shows me the same code load_file($target_url); foreach($html->find(‘a’) as $link){ echo $link->href."
"; } ?> on the browser and not the desired result. I am working on a website using xampp. Php is also properly configured.
<?php
include_once('/simple.php');
$target_url = "http://www.example.com/";
$html = new simple_html_dom();
$html->load_file($target_url);
foreach($html->find(‘a’) as $link){
echo $link->href."<br />";
}
?>
For some reason it seems like the PHP parser interprets the -> in $html->load_file( as if it should close PHP parsing, thus showing you the rest of the code as HTML.
it interprets -> as if it was a ?>
The server is probably misconfigured, or some strange option is in effect.
Other PHP files may work fine if they do not contain -> so if not using objects
If they are allowed on your server, you could try alternative syntax:
<script language="php">
echo "This is HTML script tags.";
</script>
Or ASP style syntax
<%
echo 'This is ASP like style';
%>
Although ugly it might solve your problem
PHP or your server is not properly configured because instead of executing the code, it is simply outputting it to the browser.
The reason you only see the load_file() code onwards, is because the browser interprets the > on that line as a closing HTML tag. If you view->source in your browser, you'll see the full code.
The curly quotes! The curly quotes!
They show the file has been "treated" by the wrong program (Word???), so there is another char that gets modified usually by those nasty softwares, and it is the DASH (-)!
Your dash in $html->load is probably not a real dash "-" but some similar looking charachter like the long dash "—".
Look at the difference:
----------
real dashes -
––––––––––
alternate dash –
For some reason some parser (ftp?webserver?php?) gets confused by this unexpected char, and outputs a ? instead!
And this ? near the > gives a ?> that closes PHP!!!
Rewrite your file using a proper editor, just delete the dashes and write them again and it will work.
Related
the tech guy at one of my clients raised to me I wasn't using <?php everywhere and would need to change that because of security reasons. Fair point, will do my good sir! However, he also asked if I could change ?> to php?> as the closing tags and that seemed a bit odd to me. I have never seen that, nor any mention of it anywhere (Google gave me a total of 0 relevant results). And even my code editor implies that that is faulty code.
Can anyone enlighten me if it even exists and if so what benefits it offers above ?>
Thanks in advance.
Given:
<?php
error_reporting(E_ALL);
echo "echo ";
php?>
The output is:
echo
Notice: Use of undefined constant php - assumed 'php' in /tmp/test.php on line 4
In php?>, the php part is not a component of the "end of PHP code" marker. It is just a constant that you haven't defined just like in junk?>.
The PHP tags docs clearly says that <?php is an opening tag and ?> is closing tag.
You can use short opening tag <? but you need to enable short_open_tag in your php.ini file first.
However in some cases it's good to omit the closing tag.
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
This must be terribly well known, but I can't find anything relevant. Strings in php cant have the characters < and > in them in the wamp environment. It all works fine in the live server. e g under wamp
$teststring = 'aaa<bbb';
echo $teststring;
produces aaa.
I want to edit html files using str_replace() and preg_replace().
I guess I have to modify the php set up but I don't know how.
I assume it doesn't have anything to do with WAMP, and I'm not even sure I understand the situation fully, because you say "Strings in php cant have the characters <> in them in the wamp environment" (I interpret it as: It doesn't work with WAMP) and then directly afterwards "It all works fine in the live server. e g under wamp" (I interpret it as: It does work with WAMP).
But I believe the problem is just that you are adding stray unencoded <'s into the HTML output. Think about what would happen normally:
<?php echo "I can write <em>emphasized</em> text!"; ?>
...would result in:
I can write emphasized text!
...and not:
I can write <em>emphasized</em> text!
Because you can output HTML from PHP, and the browser will read it as it would read any static HTML page. Now if you just include a random <, it will be interpreted as HTML as said in the comments and will not be valid.
So, in order to have a literal < shown in the browser, it has to be encoded as HTML entity, in this case <, e.g. 3 < 4 instead of 3 < 4. This can be automatically done using the function htmlentities. For example:
<?php echo htmlentities("This is a string with < and > and & and other stuff like this which has to be encoded."); ?>
I’m trying to implement a simple service in PHP. The service needs to return strings to certain requests. However, when I try to echo the string, PHP somehow adds \r\n to the beginning of the string. I am using echo to output the response.
I tried to echo one space character:
$test = ' ';
echo $test;
and in the response I still got '\r\n'.
I have tried header('Content-type: text'); and $string = preg_replace("\r\n", "", $string);, but I’m still getting the new line in the response.
I’m new to PHP, so if this is some kind of concept, could someone provide me with pointers to information where I can read about it?
It's possible this character is in your sourcecode somewhere, for instance at the end of one your scripts, like: ?>\r\n.
A best practice is to never include the final ?> in each file to avoid accidental output.
This recommendation is in the PSR-2 Coding Style Guide.
The closing ?> tag MUST be omitted from files containing only PHP.
It's also in the manual.
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
This question already has answers here:
Why would one omit the close tag?
(14 answers)
Closed 8 years ago.
OK, I hate doing something for no reason, especially if it appears to be the stupidest thing I've ever seen, so here goes:
I'm encountering a codebase where the PHP files start with <?php, but don't end with ?>
I have not seen any documentation on why, but apparently this has something to do with "security".
Can someone enlighten me as to why I would break with common sense and leave out the closing PHP tag at the bottom of a file?
The documentation states:
If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script.
It has nothing to do with "security". It has something to do with functions whose behaviour depends on whether output has already been sent to the client or not. The best example is the function header(). It is meant for manipulating the HTTP response headers. This function will work only before any output has been send - as in HTTP there headers cannot being sent after the body.
Let's get back to the nature of PHP. It is a scripting language which can be embedded into other documents, like HTML:
<html>
<head><title><?php echo $title; ?></title></head>
<body><?php echo $body; ?></body>
</html>
When embedded into other documents PHP's output will be inserted into the document, leaving the original document as-is, meaning just sending it's literal content to the client.
When you have a class file, for example:
<?php
class Foo {
}
?><whitespace>...
<newline>
<newline>
... you are closing the PHP tag and have two forgotten spaces and new lines in the file. PHP would send those spaces and new lines to the client, meaning a function like header() wouldn't work anymore. This simply a text document with embedded PHP code. (Unlike source code files in other languages). PHP will replace the part between the <?php ?> and send the results + remaining parts of the file to the client.
If you omit the closing PHP tag in this case, the PHP parser would just ignore the spaces and newlines because they don't contain code.
According to php.net reason behind avoiding ending php tag is:
"If a file is pure PHP code, it is preferable to omit the PHP closing tag at the end of the file. This prevents accidental whitespace or new lines being added after the PHP closing tag, which may cause unwanted effects because PHP will start output buffering when there is no intention from the programmer to send any output at that point in the script."
I got some strange problem where i have 2 copies of same website in 2 different folders. In one of those 2 copies has whitespace before doctype declaration so i have problems to work with facebook as document is formated incorrectly.
Before html there is some php calculations, but there is no echo statements or something.
What could be cause for 2 identique websites under same server in different folders, one having this issue?
I'm almost positively certain that you have some trailing whitespace at the end of a PHP include. Check there first.
Example:
<?php
// header file
include 'other_file.php';
?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- etc. -->
Then
<?php
// other_file.php
// connects to database, or something,
// but notice the whitespace after the closing tag
?>
... pretend this text isn't here
I added the note at the end to get stackoverflow's markdown editor to show the additional lines after the closing ?> tag. PHP automatically truncates one newline character after the tag in includes, but if you have more than one they will be interpreted as blank space before your doctype declaration.
Easiest fix? Remove the closing tag ?> from all of your includes. This is a valid technique, and could be the fastest way for you to get up and running.
You don't say which browser you're having problems with. IE used to check the first few letters of the page for a doctype and, if it isn't one (such as whitespace), it would go into quirks mode.
This is especially common if you are using includes. Check over all your includes or other php files carefully that come before your output or headers.
<?
echo "php here";
?>
This will cause a problem if it's in an includes
that comes before your headers or doctypes.
Remove any non php here, file should end with "?>"
not whitespace, linefeeds or characters.
As for why? Unknown, on one production server this issue raises it's head constantly for me (CentOs 5) but on my dev machine (latest Fedora) it doesn't happen and I have no issues.
Honestly there's probably something I could track down to find out why, but since proper usage says "no extra spaces or lines" I just do that and don't worry too much about the "why is it handled differently on my servers" all that much. (Kind of a cop-out I know)
I ran into this where a line break was above the Doctype. I tried a few things and then ended up just placing the doctype in the php.
<?php
echo '<!DOCTYPE html>';
... other php code/includes/etc ...
?>
<html>
... other html elements ...
Not sure if that's the right way to go about it, but it worked for me. If you're using a more extensive doctype, you'll need to escape special characters.
I just spent a day solving this problem where my libraries worked fine on static pages but under a dynamic system with a master script at the start (apache mod_write rewriting all to master script) the browser flipped into backward compat mode (js: alert(document.compatMode()) ) and distorted my styling. All my include files were ended properly and no echos, etc.. The problem? A blank line above the opening <?php of the master script.......
You may not believe but I have a MAGIC way to solve this problem:
Upload your documents (which are included before <!DOCTYPE) on your host using File Manager of CPanel, then choose the document and Click "Edit" to see the source code of the document. Then press Ctrl+Home then press Delete!
You see that nothing will be deleted but that hidden white space!!
Save your document and you will notice that everything is OK now.
I do this for all of my projects
In my case this works fine:
$ brew update
$ brew install coreutils
$ cd directoryWithSymfony2Project
$ for file in $(find ./ -name '*.twig' -or -name '*.php'); do sed `echo -e 's/[\xC2\xA0]//g'` $file > temp; echo 'Processing ' $file;rm $file; mv temp $file; done
I had the same issue, and the culprit was a whitespace at the top of one of my included PHP files, before <?php:
<?php //Line above this line was the problem
function gggl_vertical_cards(){
ob_start();
?>
//...
do you use session_start() or header() anywhere? then (without outbutbuffering) no whitespace or other character is allowed to send to the client before this functions.