PHP cURL to get dynamic content - php
I am trying to use cURL and PHP to scrape proxies off of a webpage. However, when I use cURL all I get is the CSS in the $content. The page uses wordpress so it dynamically loads content but I haven't found anything to help me download the dynamic content. I use wget in linux and the page downloads fine.
<?php
//$source1 = file_get_contents('http://www.new-fresh-proxies.blogspot.com/');
$source1 = get_data("http://www.new-fresh-proxies.blogspot.com/");
$array = array();
$source1 = preg_grep('/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}\b/', $array);
//download webpage
function get_data($url) {
$options = array(
CURLOPT_RETURNTRANSFER => 1, // return web page
CURLOPT_HEADER => true, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
CURLOPT_TIMEOUT => 120, // timeout on response
CURLOPT_MAXREDIRS => 50, // stop after 10 redirects
);
$ch = curl_init( $url );
curl_setopt_array( $ch, $options );
$content = curl_exec( $ch );
$err = curl_errno( $ch );
$errmsg = curl_error( $ch );
$header = curl_getinfo( $ch );
curl_close( $ch );
$header['errno'] = $err;
$header['errmsg'] = $errmsg;
$header['content'] = $content;
return $header;
My output:
(
string:203221) HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Expires: Wed, 06 Feb 2013 22:09:23 GMT
Date: Wed, 06 Feb 2013 22:09:23 GMT
Cache-Control: private, max-age=0
Last-Modified: Wed, 06 Feb 2013 20:39:30 GMT
ETag: "c6675d47-80ec-48ee-9c0f-613c9419f172"
Content-Encoding: gzip
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 47132
Server: GSE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html dir='ltr' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'>
<head>
<meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/>
<script type="text/javascript">(function() { var a=window,b="jstiming",d="tick";var e=function(c){this.t={};this.tick=function(c,p,h){h=void 0!=h?h:(new Date).getTime();this.t[c]=[h,p]};this[d]("start",null,c)},f=new e;a.jstiming={Timer:e,load:f};if(a.performance&&a.performance.timing){var g=a.performance.timing,j=a[b].load,k=g.navigationStart,l=g.responseStart;0<k&&l>=k&&(j[d]("_wtsrt",void 0,k),j[d]("wtsrt_","_wtsrt",l),j[d]("tbsd_","wtsrt_"))}
try{var m=null;a.chrome&&a.chrome.csi&&(m=Math.floor(a.chrome.csi().pageT),j&&0<k&&(j[d]("_tbnd",void 0,a.chrome.csi().startE),j[d]("tbnd_","_tbnd",k)));null==m&&a.gtbExternal&&(m=a.gtbExternal.pageT());null==m&&a.external&&(m=a.external.pageT,j&&0<k&&(j[d]("_tbnd",void 0,a.external.startE),j[d]("tbnd_","_tbnd",k)));m&&(a[b].pt=m)}catch(n){};a.tickAboveFold=function(c){var i=0;if(c.offsetParent){do i+=c.offsetTop;while(c=c.offsetParent)}c=i;750>=c&&a[b].load[d]("aft")};var q=!1;function r(){q||(q=!0,a[b].load[d]("firstScrollTime"))}a.addEventListener?a.addEventListener("scroll",r,!1):a.attachEvent("onscroll",r);
})();</script>
<meta content='true' name='MSSmartTagsPreventParsing'/>
<meta content='blogger' name='generator'/>
<link href='http://www.new-fresh-proxies.blogspot.com/favicon.ico' rel='icon' type='image/x-icon'/>
<link href='http://new-fresh-proxies.blogspot.com/' rel='canonical'/>
<link rel="alternate" type="application/atom+xml" title="New Fresh Proxies - Atom" href="http://new-fresh-proxies.blogspot.com/feeds/posts/default" />
<link rel="alternate" type="application/rss+xml" title="New Fresh Proxies - RSS" href="http://new-fresh-proxies.blogspot.com/feeds/posts/default?alt=rss" />
<link rel="service.post" type="application/atom+xml" title="New Fresh Proxies - Atom" href="http://www.blogger.com/feeds/2001908494944967755/posts/default" />
<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.blogger.com/rsd.g?blogID=2001908494944967755" />
<link rel="openid.server" href="http://www.blogger.com/openid-server.g" />
<link rel="openid.delegate" href="http://new-fresh-proxies.blogspot.com/" />
<!--[if IE]> <script> (function() { var html5 = ("abbr,article,aside,audio,canvas,datalist,details," + "figure,footer,header,hgroup,mark,menu,meter,nav,output," + "progress,section,time,video").split(','); for (var i = 0; i < html5.length; i++) { document.createElement(html5[i]); } try { document.execCommand('BackgroundImageCache', false, true); } catch(e) {} })(); </script> <![endif]-->
<title>New Fresh Proxies</title>
<link type='text/css' rel='stylesheet' href='//www.blogger.com/static/v1/widgets/3950009988-widget_css_bundle.css' />
<link type="text/css" rel="stylesheet" href="//www.blogger.com/dyn-css/authorization.css?targetBlogID=2001908494944967755&zx=c6675d47-80ec-48ee-9c0f-613c9419f172"/>
<style id='page-skin-1' type='text/css'><!--
/*
-----------------------------------------------
Theme Name: Harmonika
Theme URL: http://newwpthemes.com/wordpress-theme/harmonika/
Description: Harmonika is a free WordPress theme with options page and supports the post thumbnails. Suitable for any niche.
Author: NewWpThemes.com
Author URI: http://newwpthemes.com/
Version: 1.0
Tags: Ads Ready, Two Columns, Right Sidebar, Fixed Width, Blogging, Options Page
Template Name: Harmonika
Template URI: http://themecraft.net/2010/06/harmonika-blogger-template
Version: 1.0
Author: Theme Craft
Author URI: http://www.themecraft.net
Harmonika Blogger Template comes under a Creative Commons Attribution-Noncommercial-Share Alike 2.5 Malaysia License.
This means it is free to use on your blog, and you must keep the footer link intact, also as a respect to the designer and converter.
We sincerely need your respect to continue our free Blogger template production, thank you.
ThemeCraft.net
----------------------------------------------- */
/* Use this with templates/template-twocol.html */
#navbar-iframe{
height:0px;
visibility:hidden;
display:none;
}
/* -----------------------------------------------------------------------
Blueprint CSS Framework 0.8
http://blueprintcss.org
* Copyright (c) 2007-Present. See LICENSE for more info.
* See README for instructions on how to use Blueprint.
* For credits and origins, see AUTHORS.
* This is a compressed file. See the sources in the 'src' directory.
----------------------------------------------------------------------- */
/* reset.css */
html, body, div, span, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, code, del, dfn, em, img, q, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td {margin:0;padding:0;border:0;font-weight:inherit;font-style:inherit;font-size:100%;font-family:inherit;}
body {line-height:1.5;}
caption, th, td {text-align:left;font-weight:normal;}
blockquote:before, blockquote:after, q:before, q:after {content:"";}
blockquote, q {quotes:"" "";}
img {max-width: 100%; /* not working in IE6*/}
a img {max-width: 100%; /* not working in IE6*/ border:none;}
/* typography.css */
body {font-size:75%;color:#222;background:#000;font-family:"Helvetica Neue", Arial, Helvetica, sans-serif;}
h1, h2, h3, h4, h5, h6 {font-weight:normal;}
h1 {font-size:3em;line-height:1;margin-bottom:0.5em;}
h2 {font-size:2em;margin-bottom:0.75em;}
h3 {font-size:1.5em;line-height:1;margin-bottom:1em;}
h4 {font-size:1.2em;line-height:1.25;margin-bottom:1.25em;}
h5 {font-size:1em;font-weight:bold;margin-bottom:1.5em;}
h6 {font-size:1em;font-weight:bold;}
h1 img, h2 img, h3 img, h4 img, h5 img, h6 img {margin:0;}
p {margin:0 0 1.5em;}
p img.left {float:left;margin:1.5em 1.5em 1.5em 0;padding:0;}
p img.right {float:right;margin:1.5em 0 1.5em 1.5em;}
a:focus, a:hover {color:#000;}
a {color:#009;text-decoration:underline;}
blockquote {margin:1.5em;color:#666;font-style:italic;}
strong {font-weight:bold;}
em, dfn {font-style:italic;}
dfn {font-weight:bold;}
sup, sub {line-height:0;}
abbr, acronym {border-bottom:1px dotted #666;}
address {margin:0 0 1.5em;font-style:italic;}
del {color:#666;}
pre {margin:1.5em 0;white-space:pre;}
pre, code, tt {font:1em 'andale mono', 'lucida console', monospace;line-height:1.5;}
li ul, li ol {margin:0 1.5em;}
ul, ol {margin:0 1.5em 1.5em 1.5em;}
ul {list-style-type:disc;}
ol {list-style-type:decimal;}
dl {margin:0 0 1.5em 0;}
dl dt {font-weight:bold;}
dd {margin-left:1.5em;}
table {margin-bottom:1.4em;width:100%;}
th {font-weight:bold;}
thead th {background:#c3d9ff;}
tr.even td {background:#e5ecf9;}
tfoot {font-style:italic;}
caption {background:#eee;}
.small {font-size:.8em;margin-bottom:1.875em;line-height:1.875em;}
.large {font-size:1.2em;line-height:2.5em;margin-bottom:1.25em;}
.hide {display:none;}
.quiet {color:#666;}
.loud {color:#000;}
.highlight {background:#ff0;}
.added {background:#060;color:#fff;}
.removed {background:#900;color:#fff;}
.first {margin-left:0;padding-left:0;}
.last {margin-right:0;padding-right:0;}
.top {margin-top:0;padding-top:0;}
.bottom {margin-bottom:0;padding-bottom:0;}
/* forms.css */
label {font-weight:bold;}
fieldset {padding:1.4em;margin:0 0 1.5em 0;border:1px solid #ccc;}
legend {font-weight:bold;font-size:1.2em;}
input.text, input.title, textarea, select {margin:0.5em 0;border:1px solid #bbb;}
input.text:focus, input.title:focus, textarea:focus, select:focus {border:1px solid #666;}
input.text, input.title {width:300px;padding:5px;}
input.title {font-size:1.5em;}
textarea {width:390px;height:250px;padding:5px;}
.error, .notice, .success {padding:.8em;margin-bottom:1em;border:2px solid #ddd;}
.error {background:#FBE3E4;color:#8a1f11;border-color:#FBC2C4;}
.notice {background:#FFF6BF;color:#514721;border-color:#FFD324;}
.success {background:#E6EFC2;color:#264409;border-color:#C6D880;}
.error a {color:#8a1f11;}
.notice a {color:#514721;}
.success a {color:#264409;}
/* grid.css */
.container {width:950px;margin:0 auto;}
.showgrid {background:url(src/grid.png);}
.column, div.span-1, div.span-2, div.span-3, div.span-4, div.span-5, div.span-6, div.span-7, div.span-8, div.span-9, div.span-10, div.span-11, div.span-12, div.span-13, div.span-14, div.span-15, div.span-16, div.span-17, div.span-18, div.span-19, div.span-20, div.span-21, div.span-22, div.span-23, div.span-24 {float:left;margin-right:10px;}
.last, div.last {margin-right:0;}
.span-1 {width:30px;}
.span-2 {width:70px;}
.span-3 {width:110px;}
.span-4 {width:150px;}
.span-5 {width:190px;}
.span-6 {width:230px;}
.span-7 {width:270px;}
.span-8 {width:310px;}
.span-9 {width:350px;}
.span-10 {width:390px;}
.span-11 {width:430px;}
.span-12 {width:470px;}
.span-13 {width:510px;}
.span-14 {width:550px;}
.span-15 {width:590px;}
.span-16 {width:630px;}
.span-17 {width:670px;}
.span-18 {width:710px;}
.span-19 {width:750px;}
.span-20 {width:790px;}
.span-21 {width:830px;}
.span-22 {width:870px;}
.span-23 {width:910px;}
.span-24, div.span-24 {width:950px;margin:0;}
input.span-1, textarea.span-1, input.span-2, textarea.span-2, input.span-3, textarea.span-3, input.span-4, textarea.span-4, input.span-5, textarea.span-5, input.span-6, textarea.span-6, input.span-7, textarea.span-7, input.span-8, textarea.span-8, input.span-9, textarea.span-9, input.span-10, textarea.span-10, input.span-11, te...
Curl wont be able to get it directly since it wont execute javascript. But if its coming from an ajax request, you can make a request to that endpoint directly.
Use dev tools/firebug to see what is happening.
Couple things:
Where is your 'output' coming from? I see no displays in your code ...
I also think your preg_grep statement is incorrect. You're searching a blank array and saving the result to the variable you just pulled your data into. Try:
$array = preg_grep('/\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}\b/', $source1);
When I run the code and dump $source1['content'] directly after the get_data call, I get a crap-ton of IP addresses ...
It seems to me like either a timeout or a problem with your Regexp.
Why not stick to file_get_contents like you tried in the first place?
$content = file_get_contents('http://www.new-fresh-proxies.blogspot.com.au');
preg_match_all('/(\d+\.\d+\.\d+\.\d+(:\d+)?)/', $content, $matches);
print_r($matches[1]);
This will print out a list of IPs:
Array
(
[0] => 1.204.168.15:6673
[1] => 1.234.45.130:80
[2] => 1.34.163.101:8080
[3] => 1.34.29.89:8080
[4] => 1.34.8.221:3128
....
Hope that helps.
Related
Convert PDF documents (.pdf) to EXCEL document (.XLXS) using PHP
I have a set of pdf documents and I need to convert the files to excel. When I tried to convert those I could not convert it was stating failed. So I have tried to convert to HTML tag all the data are converting but it was changing the whole document into P tags. I have attached the sample response of HTML tags. Please suggest me some ides for converting pdf to excel in php. I have used this library <p class="ft00" style="margin: 0; padding: 0; font-size: 11px; font-family: Times; color: #000000; position: absolute; top: 863px; left: 683px; white-space: nowrap;"><b>10</b></p> <p class="ft01" style="margin: 0; padding: 0; font-size: 9px; font-family: Times; color: #000000; -moz-transform: matrix( 0, -1, 0.9999875, 0, 0, 0); -webkit-transform: matrix( 0, -1, 0.9999875, 0, 0, 0); -o-transform: matrix( 0, -1, 0.9999875, 0, 0, 0); -ms-transform: matrix( 0, -1, 0.9999875, 0, 0, 0); -moz-transform-origin: left 75%; -webkit-transform-origin: left 75%; -o-transform-origin: left 75%; -ms-transform-origin: left 75%; position: absolute; top: 207px; left: 63px; white-space: nowrap;"><b>THE</b></p> Library: PdfToHtml $pdf = new \TonchikTm\PdfToHtml\Pdf($pdf_file , [ 'pdftohtml_path' => $bindir, 'pdfinfo_path' => $binexe, 'outputDir' => getcwd().'/excelfile/'.uniqid() // output dir ]); $newpage = ''; foreach ($pdf->getHtml()->getAllPages() as $page) { $newpage .= $page . '<br/>'; }
I've never had a use case to convert PDF to Excel, but what other libraries did you try? PDFTables looks promising: https://pdftables.com/pdf-to-excel-api#php https://github.com/pdftables/php-pdftables-api If it needs to be done with the PDF to HTML library you're using, I don't know of an easy/clean way to do this. Based on the Question, each page returns a string containing all the paragraph elements? If returns an array of elements, you should be able to loop over each paragraph and use strip_tags() to get the individual values. If it's a string, look into using Simple HTML Dom to parse the HTML elements from string format so that you can work with the HTML more easily. $html = str_get_html('<div id="hello">Hello</div><div id="world">World</div>');
Wkhtmltopdf conversion
My html to pdf conversion using wkhtmlpdf working fine without the <style> tag but when I include the style tag it's not working that means not generating the pdf some one help me to solve this issue or let me know how to include my style.css in this format.
The way I handle this is as follows-- <?php // create some HTML content $html = '<!DOCTYPE html><html><head> <style> body { margin: 0; padding: 0; } ... </style></head><body> <!-- Your body content --> </body></html>'; $options = array( "no-outline", "binary" => "/usr/local/bin/wkhtmltopdf", "margin-top" => ".25in", "margin-right" => ".25in", "margin-bottom" => ".55in", "margin-left" => ".25in", "zoom" => 1 ); $footer = '<!DOCTYPE html><html><head></head><body>...</body></html>'; $page_options = array('footer-html' => $footer); $pdf->addPage($html, $page_options); $pdf->saveAs('MY_PDF_NAME.pdf'); Hope this helps! Best, -Rush
phpwkhtmltopdf is generating multiple pages instead of one
I want to show the html in pdf only on one page. But it makes it on multiple pages. I tried to add css modifications, but it didn't work eather. There are plently of options to choose, but cant find one that suits. options avaliable https://github.com/mikehaertl/phpwkhtmltopdf http://wkhtmltopdf.org/usage/wkhtmltopdf.txt php $options = array( 'no-outline', // Make Chrome not complain 'margin-top' => 0, 'margin-right' => 0, 'margin-bottom' => 0, 'margin-left' => 0, // Default page options 'disable-smart-shrinking', 'user-style-sheet' => PATH . '/assets/pdf.css', ); pdf.css table:first { position: relative; top: 1cm; left: 1cm; width: 2cm; height: 1.6cm; } table { font-size: 10px; font-family: Arial; }
How do I compress HTML in laravel 5
In Laravel 4.0, I use the code below to compress the HTML laravel response outputs to browser, however it doesn't work in laravel 5. App::after(function($request, $response) { if($response instanceof Illuminate\Http\Response) { $buffer = $response->getContent(); if(strpos($buffer,'<pre>') !== false) { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\r/" => '', "/>\n</" => '><', "/>\s+\n</" => '><', "/>\n\s+</" => '><', ); } else { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\n([\S])/" => '$1', "/\r/" => '', "/\n/" => '', "/\t/" => '', "/ +/" => ' ', ); } $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer); $response->setContent($buffer); } }); Please how do i make this work in Laravel 5. OR Please provide a better way of compressing HTML in laravel 5 if any. Thanks in advance. NB: I don't wish to use any laravel package for compressing html, just need a simple code that does the work without killing performance.
Complete code is this (with custom GZip enabled) : <?php namespace App\Http\Middleware; use Closure; class OptimizeMiddleware { /** * Handle an incoming request. * * #param \Illuminate\Http\Request $request * #param \Closure $next * #return mixed */ public function handle($request, Closure $next) { $response = $next($request); $buffer = $response->getContent(); if(strpos($buffer,'<pre>') !== false) { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\r/" => '', "/>\n</" => '><', "/>\s+\n</" => '><', "/>\n\s+</" => '><', ); } else { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\n([\S])/" => '$1', "/\r/" => '', "/\n/" => '', "/\t/" => '', "/ +/" => ' ', ); } $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer); $response->setContent($buffer); ini_set('zlib.output_compression', 'On'); // If you like to enable GZip, too! return $response; } } Please check your browser network inspector for Content-Length header before/after implement this code. enjoy it ... :).. .
It is not very good solution to minify html in middleware as you can spend a lot of CPU time on it and it runs on every request. Instead it is better to use htmlmin package ( https://github.com/HTMLMin/Laravel-HTMLMin ): composer require htmlmin/htmlmin php artisan vendor:publish Minifying HTML on blade template level and caching it in storage should be much more effective.
The recommended way to do this in Larvel 5 is to rewrite your function as middleware. As stated in the docs: ..this middleware would perform its task after the request is handled by the application: <?php namespace App\Http\Middleware; class AfterMiddleware implements Middleware { public function handle($request, Closure $next) { $response = $next($request); // Perform action return $response; } }
I have created a webpack plugin to solve same purpose.MinifyHtmlWebpackPlugin Install the plugin with npm: $ npm install minify-html-webpack-plugin --save-dev For Laravel Mix Users Paste below snippets into mix.js file. const MinifyHtmlWebpackPlugin = require('minify-html-webpack-plugin'); const mix = require('laravel-mix'); mix.webpackConfig({ plugins: [ new MinifyHtmlWebpackPlugin({ src: './storage/framework/views', ignoreFileNameRegex: /\.(gitignore)$/, rules: { collapseWhitespace: true, removeAttributeQuotes: true, removeComments: true, minifyJS: true, } }) ] }); It will minify all view files during the Webpack build.
This is almost a copy of Vahid's answer but it fixes two problems. 1) It checks if a response is a BinaryFileResponse as any attempt to modify this type of response will throw an Exception. 2) It retained newline characters as the complete elimination of newlines will lead to bad Javascript code on lines with single-line comment. For example, the code below var a; //This is a variable var b; //This will be commented out Will become var a; //This is a variable var b; //This will be commented out Note: At the time of this answer I couldn't get my hands on a good regex to match single line comments without complications or rather, ignore newlines on only lines with a single-line comment, so I'm hoping for a better fix. Here's the modified version. <?php namespace App\Http\Middleware; use Closure; class OptimizeMiddleware { /** * Handle an incoming request. * * #param \Illuminate\Http\Request $request * #param \Closure $next * #return mixed */ public function handle($request, Closure $next) { $response = $next($request); if ($response instanceof \Symfony\Component\HttpFoundation\BinaryFileResponse) { return $response; } else { $buffer = $response->getContent(); if (strpos($buffer, '<pre>') !== false) { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\r/" => '', "/>\n</" => '><', "/>\s+\n</" => '><', "/>\n\s+</" => '><', ); } else { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\n([\S])/" => '$1', "/\r/" => '', "/\n+/" => "\n", "/\t/" => '', "/ +/" => ' ', ); } $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer); $response->setContent($buffer); ini_set('zlib.output_compression', 'On'); //enable GZip, too! return $response; } } } Edit Compressing output for every request using the middleware truly is really a bad idea, I recommend you check out this solution by Jokerius
Just in case you are rendering the view manually: echo view('example.site')->render(function($view, $content) { return preg_replace( ['/\>[^\S ]+/s', '/[^\S ]+\</s', '/(\s)+/s'], ['>', '<', '\\1'], $content ); } );
This package is much better option in my opinion renatomarinho/laravel-page-speed
I did it with very simple code. Example: welcome.blade.php Add the following code to the beginning of the page <?php ob_start('compress_page');?> Add the following code to the end of the page: <?php ob_end_flush(); function compress_page($buffer) { $search = array("/>[[:space:]]+/", "/[[:space:]]+</"); $replace = array(">","<"); return preg_replace($search, $replace, $buffer); }?> Full page code example: <?php ob_start('compress_page');?> <!doctype html> <html lang="{{ app()->getLocale() }}"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Laravel</title> <!-- Fonts --> <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css"> <!-- Styles --> <style> html, body { background-color: #fff; color: #636b6f; font-family: 'Raleway', sans-serif; font-weight: 100; height: 100vh; margin: 0; } .full-height { height: 100vh; } .flex-center { align-items: center; display: flex; justify-content: center; } .position-ref { position: relative; } .top-right { position: absolute; right: 10px; top: 18px; } .content { text-align: center; } .title { font-size: 84px; } .links > a { color: #636b6f; padding: 0 25px; font-size: 12px; font-weight: 600; letter-spacing: .1rem; text-decoration: none; text-transform: uppercase; } .m-b-md { margin-bottom: 30px; } </style> </head> <body> <div class="flex-center position-ref full-height"> #if (Route::has('login')) <div class="top-right links"> #auth Home #else Login Register #endauth </div> #endif <div class="content"> <div class="title m-b-md"> Laravel </div> <div class="links"> Documentation Laracasts News Forge GitHub </div> </div> </div> </body> </html> <?php ob_end_flush(); function compress_page($buffer) { $search = array("/>[[:space:]]+/", "/[[:space:]]+</"); $replace = array(">","<"); return preg_replace($search, $replace, $buffer); }?>
this is best way.. we don't need to use laravel packeges .Thanks.. <?php namespace App\Http\Middleware; use Closure; class OptimizeMiddleware { /** * Handle an incoming request. * * #param \Illuminate\Http\Request $request * #param \Closure $next * #return mixed */ public function handle($request, Closure $next) { $response = $next($request); $buffer = $response->getContent(); if(strpos($buffer,'<pre>') !== false) { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\r/" => '', "/>\n</" => '><', "/>\s+\n</" => '><', "/>\n\s+</" => '><', ); } else { $replace = array( '/<!--[^\[](.*?)[^\]]-->/s' => '', "/<\?php/" => '<?php ', "/\n([\S])/" => '$1', "/\r/" => '', "/\n/" => '', "/\t/" => '', "/ +/" => ' ', ); } $buffer = preg_replace(array_keys($replace), array_values($replace), $buffer); $response->setContent($buffer); ini_set('zlib.output_compression', 'On'); // If you like to enable GZip, too! return $response; } }
For easy compression, I build my own laravel module. This module will compress all the final html output before sending to the client (browser). You can also target multiple environment at a time using .env file. More details on how to install and configure will be found here
Formatting tables generated with PHP/PEAR Structures_DataGrid & HTML_Table
I've been working through some examples in a book using pear for the first time... so far it seems pretty cool, but I'm having a small problem where I need to tweak the formatting of one particular column of the table generated from Structures_DataGrid & HTML_Table - specifically I need it to not wrap the text in a cell at the hyphen - which I think I could do using 'nowrap' and html/css - but so far I'm not seeing how to pass that sort of formatting to just one column in the table... Any suggestions? Added source code below: <?php // Include the DB access credentials require 'dbcred.php'; // Include the PEAR Structures_DataGrid class require 'Structures/DataGrid.php'; // instantiate grid for 10 records per page $datagrid = new Structures_DataGrid(10); // Define our Datasource options, in this case PDO MySQL $options = array('dsn' => "mysql://$user:$password#$db_host/$db_name"); // Define the Query $sql = "SELECT * FROM contact_info"; // Bind the Query to our Datagrid $bind = $datagrid->bind($sql, $options); // Test for Errors if (PEAR::isError($bind)) { /*error_log('DataGrid Error: '. $bind->getMessage()); $gridsource = 'Foo';*/ echo $bind -> getMessage(); } else { // Define our Column labels, using a 'column' => 'Label' format $columns = array( 'title' => 'Title', 'first_name' => 'First Name', 'mid_init' => 'Mid. Init.', 'last_name' => 'Last Name', 'suffix' => 'Suffix', 'street_address' => 'Street Address', 'city' => 'City', 'state_prov' => 'State/Province', 'post_code' => 'Postal Code', 'phone_pri' => 'Phone (Pri)', 'phone_alt' => 'Phone (Alt)', 'email' => 'E-Mail', ); $datagrid->generateColumns($columns); // Some more options, for our renderer $renderer_options = array( 'sortIconASC' => '⇑', 'sortIconDESC' => '⇓', 'headerAttributes' => array('bgcolor' => '#E3E3E3'), 'evenRowAttributes' => array('bgcolor' => '#A6A6A6'), ); $datagrid->setRendererOptions($renderer_options); // Add some final attributes to our table $renderer = $datagrid->getRenderer(); $renderer->setTableAttribute('cellspacing', 0); $renderer->setTableAttribute('cellpadding', 5); $renderer->setTableAttribute('border', 1); // Render the table, be sure to check for errors $gridbody = $datagrid->getOutput(); if (PEAR::isError($gridbody)) { error_log('DataGrid render error: ' . $gridbody->getMessage()); $gridbody = ''; } // Finally, render the pager, again checking for errors $gridpager = $datagrid->getOutput(DATAGRID_RENDER_PAGER); if (PEAR::isError($gridpager)) { error_log('DataGrid render error: ' . $gridpager->getMessage()); $gridpager = ''; } $gridsource = $gridbody . $gridpager; } ?> <!DOCTYPE html public "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>PEAR::Structures_DataGrid</title> <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /> <style type="text/css"> body { font-family: Tahoma, Arial, Helvetica, sans-serif; font-size: 11px; } h1 { font-size: 1.2em; color: navy } th { white-space: no-wrap; } </style> </head> <body> <h1>PEAR::Structures_DataGrid</h1> <?php echo $gridsource ?> </body> </html>
Don't know what pear is but: You can format columns like this in html: <table> <col style="background: green;" /> <col /> <tr> <td>First TD of first TR</td> <td>Second TD of first TR</td> </tr> <tr> <td>First TD of second TR</td> <td>Second TD of second TR</td> </tr> </table> That makes the background of the first column green.