php require and white space - php

I was wondering if there was a way to slightly modify the require or include functionality so that it removes line breaks and white space. So it minifies all the html / js inside the documents that im trying to grab.
I tried this:
trim(require('my document.php'));
didn't work though, is there a correct way to do this?
Cheers,
Doug

You probably want to do something like
ob_start();
require('my document.php');
echo minify(ob_get_flush());
Which will get all the output generated by my document.php and minify it, you have to find a minifying library to do it though.

You'd have to write or use a parser of some sort to do the job properly. If you can use a rough and ready solution then str_replace will take an array and replace with a single character
e.g. str_replace(array("\n", "\r", "\t", "etc"), " ", $mystring);
But that seems like a lot of processing for what you want to achieve.

this question makes no sense.
Removing whitespaces makes no sense itself. If it's such a big concern, you can set up your server to send compressed contents, it will reduce bandwith.
Yet why to clean whitespace in the included files output only? Why not to do it for the whole site output at once?

What you're describing is potentially fragile and vastly inferior to simply enabling compression.
It would be helpful if you update your question to say why you would want to do this.
However, you can do it like this:
ob_start();
require('file');
echo = preg_replace(array("#[\r\n\t]+#", '#>\s+<#', '#\s+#', '#\s?{\s?#', '#\s?}\s?#'), array('', '><', ' ', '{', '}'), ob_get_clean());
Even so you'll likely find you manage to remove whitespace that you need - especially if you are running it on javascript and miss a semicolon.

Related

Replace // comments by /* comments */ Except in URLs [duplicate]

I need to remove the comment lines from my code.
preg_replace('!//(.*)!', '', $test);
It works fine. But it removes the website url also and left the url like http:
So to avoid this I put the same like preg_replace('![^:]//(.*)!', '', $test);
It's work fine. But the problem is if my code has the line like below
$code = 'something';// comment here
It will replace the comment line with the semicolon. that is after replace my above code would be
$code = 'something'
So it generates error.
I just need to delete the single line comments and the url should remain same.
Please help. Thanks in advance
try this
preg_replace('#(?<!http:)//.*#','',$test);
also read more about PCRE assertions http://cz.php.net/manual/en/regexp.reference.assertions.php
If you want to parse a PHP file, and manipulate the PHP code it contains, the best solution (even if a bit difficult) is to use the Tokenizer : it exists to allow manipulation of PHP code.
Working with regular expressions for such a thing is a bad idea...
For instance, you thought about http:// ; but what about strings that contain // ?
Like this one, for example :
$str = "this is // a test";
This can get complicated fast. There are more uses for // in strings. If you are parsing PHP code, I highly suggest you take a look at the PHP tokenizer. It's specifically designed to parse PHP code.
Question: Why are you trying to strip comments in the first place?
Edit: I see now you are trying to parse JavaScript, not PHP. So, why not use a javascript minifier instead? It will strip comments, whitespace and do a lot more to make your file as small as possible.

Why should use linebreak '\n'

I need some advice ...
I'd like to know if it is a good practice to use in a code line breaks "\n"? What is the purpose?
$my_string .= "\n" . "<p>Some values</p>" . "\n";
Till now I haven't use it and I'd really like to know ... your opinion.
Thanks
You only need to use "\n" to make the resulting html code neater/easier to read. It is not necessary.
The new line makes your generated code easier to read. You might think noone should read your code, however if you run into some problems the generated code is easier to read for debugging purposes as well.
\n can be used when streaming over sockets as well. Sometimes you need to use \r, depending on the operating system.
This is very common mistake of the newbie programmers.
They never have an idea that the result of the PHP script execution is plain HTML code.
And sometimes a programmer have to sort things out with that HTML.
While it's just impossible if there are no linegreaks in the code.
Anyway, a good practice would be
$my_string = "Some values";
?>
<p><?=$my_string</p>
so, you won't need no special linebreaks in the PHP code.
Also, there are some cases where you have to use linebreaks.
For example, if you are composing an HTML email message, you hve to add linebreaks, or they will be forcibly added in the unexpected places.
at time of writing to break line, means display text after line-break to next line
like enter in some text editor...
The only purpose is for people using the "View Source" feature of the browser - basically no one. While it is considered good practice, I almost never do (because it's pointless) :)
for pre-tag (pre formatted text):- http://webdesign.about.com/od/htmltags/f/blfaqpre.htm
so, you can do this in SO
1
2
3
4
5
6
7
8
\n is just an escape for a new line. An escape is not really required in PHP, but it makes it seamless to output a new line mark.

how to remove the comment line starts with // and not the url like http:// using preg_replace

I need to remove the comment lines from my code.
preg_replace('!//(.*)!', '', $test);
It works fine. But it removes the website url also and left the url like http:
So to avoid this I put the same like preg_replace('![^:]//(.*)!', '', $test);
It's work fine. But the problem is if my code has the line like below
$code = 'something';// comment here
It will replace the comment line with the semicolon. that is after replace my above code would be
$code = 'something'
So it generates error.
I just need to delete the single line comments and the url should remain same.
Please help. Thanks in advance
try this
preg_replace('#(?<!http:)//.*#','',$test);
also read more about PCRE assertions http://cz.php.net/manual/en/regexp.reference.assertions.php
If you want to parse a PHP file, and manipulate the PHP code it contains, the best solution (even if a bit difficult) is to use the Tokenizer : it exists to allow manipulation of PHP code.
Working with regular expressions for such a thing is a bad idea...
For instance, you thought about http:// ; but what about strings that contain // ?
Like this one, for example :
$str = "this is // a test";
This can get complicated fast. There are more uses for // in strings. If you are parsing PHP code, I highly suggest you take a look at the PHP tokenizer. It's specifically designed to parse PHP code.
Question: Why are you trying to strip comments in the first place?
Edit: I see now you are trying to parse JavaScript, not PHP. So, why not use a javascript minifier instead? It will strip comments, whitespace and do a lot more to make your file as small as possible.

fastest way to remove whitespace from a rendered PHP file

I tried a performance check tool "DOM Monster" to analyze my php site. There is one information which says "50% of nodes are whitespace-only text nodes".
Ok I unterstand the problem but what is the fastest way to cleanup whitespace in php?
I think a good start is to use the "Output Control" like ob_start() and then replace the whitespace before releasing it with ob_end_flush(). In the moment I do everything with echo echo ... I never read much about this ob_* things is it useful?
I guess using preg_replace() is a performance killer for this job or?
So what is the best practice for this?
The fastest way to remove whitespace-only nodes is to not create them in the first place. Just remove all the whitespace immediately before and after each HTML tag.
You certainly could remove the spaces from your code after the fact using an output handler (look at the callback bit in ob_start), but if your goal is performance, then that kind of defeats the purpose.
A whitespace-only node is in the DOM tree parsed by the browser when it reads your HTML. It's where there's an HTML tag, then nothing but whitespace, then another HTML tag. It's a waste of browser resources, but not a huge deal.
The function trim() will solve your problem, isn't it?
http://www.php.net/manual/en/function.trim.php
Well, I guess you talk about HTML, and HTML is as is a meta language full of whitespace (attributes, texts).
By the way, you probably use newlines for readability.
I rather advise you to compress your page with deflate/gzip and webserver rules, ie an .htaccess rule:
<FilesMatch "\\.(js|css|html|htm|php|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
You can also take a look at Tidy which is a library to help you to check and cleanup your HTML code.
preg_replace will of course slow things down a little. But probably it's the fastest way anyway. The problem is more that preg_replace may be unreliable because it is very hard to write regular expression that works on all possible cases.
If you are createing XML/XHTML output, you could parse all your data using a fast stream parser SAX or StAX, php has both builtin usually, and then write the data back to the output without the whitespaces. That's simple, effective, reliable und at least medium fast. It's still not going to blow you off with speed.
Another option would be to just use gzip. (ob_handler('gz_handler') is the call in php if I remember correctly). This will compress your data and compression works extremely well on problems with data that repeats a lot within a document. That come with a litte performance penalty as well, but the reduced size of the output document may make up for it.
Though beware that the output will not be send to the browser before all output is available. This makes partial loading of webpages much harder ;-).
The problem with using ob_* and then trimming whitespace is that you’ll have to make sure to not remove displayed whitespace like in <pre> tags or <textarea>s etc. You’ll need a syntactical parser which understands where it should not trim.
With an (performance-)expensive parser you should also cache output where possible.
The following is code to remove all space characters but the first of a sequence of spaces. So 1 space will be kept, 3 spaces pruned to 1, etc.
at the top of you php file do
ob_start();
At the end do
function StripExtraSpace($s)
{
$newstr = "";
for($i = 0; $i < strlen($s); $i++)
{
$newstr = $newstr . substr($s, $i, 1);
if(substr($s, $i, 1) == ' ')
while(substr($s, $i + 1, 1) == ' ')
$i++;
}
return $newstr;
}
$content = ob_get_clean();
echo StripExtraSpace($content);

Debugging PHP Output

I have a php website that on certain pages is adding a dot or space before the first html tag. I can't figure out where it is coming from - is there a way to debug the code so i can see where it is coming from?
Thanks,
Josh
To help prevents this happening it is considered a good practice to don't end your PHP file with a ?>.
You possibly have some file that are this way (notice the extra space after the ?>):
<?php
// Some code //
?>
If you would remove the ?> at the end, the extra space at the end of the file won't be interpreted as something to output.
For files that contain only PHP code,
the closing tag ("?>") is never
permitted. It is not required by PHP,
and omitting it´ prevents the
accidental injection of trailing white
space into the response.
Source: http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html
Maybe it is a BOM character?
Maybe you should check your templates if you are using them... the problem could be there and not in your main code.
and yes is a GOOD PRACTICE in PHP not to close the ending tag.
There really is no good way to go about debugging this. You need to go through every file the page is hitting and figure out where the output is coming from. If you really wanted to be lazy about it you could do some output buffering, but this isn't the right way to do things.
Problems like this can be difficult to track down. If you're in some kind of framework or system that includes a lot of files, you might try a var_dump(get_included_files()) on the line before your error occurs, and that will give you a place to start. If that isn't sufficient, xdebug might get you further. Things to look out for are space before and after the PHP tags, and functions that might send output.

Categories