Skip to content

Instantly share code, notes, and snippets.

<?php
// source: http://www.apphp.com/index.php?snippet=php-compress-multiple-css-files
header('Content-type: text/css');
ob_start('compress_css');
function compress_css($buffer) {
/* remove comments in css file */
$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer);
/* also remove tabs, spaces, newlines, etc. */
@apphp
apphp / gist:ffa35d4699609bac697e
Created July 23, 2014 07:52
Error Logging in .htaccess
# display no errs to user
# source: http://www.apphp.com/index.php?snippet=htaccess-error-logging
php_flag display_startup_errors off
php_flag display_errors off
php_flag html_errors off
# log to file
php_flag log_errors on
@apphp
apphp / gist:f256626e14041adf1fdd
Created July 14, 2014 07:26
Favicon and Apple Icons in HTML
<!-- You may place favicon.ico and apple-touch-icon.png in the root of your domain and simply remove these references -->
<!-- source: http://www.apphp.com/index.php?snippet=html-favicon-and-apple-icons -->
<link rel="shortcut icon" href="/favicon.ico">
<link rel="apple-touch-icon" href="/apple-touch-icon.png">
@apphp
apphp / Targeting Chrome With CSS
Created July 7, 2014 07:26
This solution is based on Chrome specific CSS extension. Other browsers will ignore this rule.
<style type="text/css">
@media screen and (-webkit-min-device-pixel-ratio:0) {
H5 { color:red; }
P { margin-left:20px; }
/* other special styles for Chrome here */
}
</style>
// source: http://www.apphp.com/index.php?snippet=css-targeting-chrome-only
@apphp
apphp / gist:ae2f5bd22d532dbaf695
Created July 1, 2014 08:19
Create new Object From Variable in JavaScript
/*
This example of code allows you to create a new object in javascript (using simple inheritance)
such that the class of the object is defined from a variable. After creating the object you may use
it for your purposes.
source: http://www.apphp.com/index.php?snippet=javascript-create-object-from-variable
*/
<script type="text/javascript">
var className = "PluginClass";
// get a reference to the class object itself
// (we've assumed the class is defined in a global scope)
@apphp
apphp / gist:76d4fc3eaa21d982921f
Created June 23, 2014 07:28
Change Image Properties 'On-The-Fly'
Sometimes you may need to change image properties 'on-the-fly'. The best example is when you want to display blog post short description, retrieve the first image and try to turn it into the thumbnail: change some properties and add a special class name. source: http://www.apphp.com/index.php?snippet=php-change-image-properties-on-fly
<?php
$postText = '..includes html + image tags';
// find first image and redo it
preg_match_all('/<img[^>]+>/i', $postText, $images);
$postThumbnail = isset($images[0][0]) ? $images[0][0] : '';
$postThumbnail = preg_replace('/(width|height|style)="*"/', '', $postThumbnail);
$postThumbnail = preg_replace('/<img/', '<img class="blog-post-thumbnail"', $postThumbnail);
@apphp
apphp / gist:3c23b9cdbc42432afcd9
Created June 23, 2014 07:27
Change Language Automatically According to the Visitor Country
This simple code demonstrates how to change automatically site language, according to the visitor's country. If you implement this script in your site it will open the page in the language of your visitor.
source: http://www.apphp.com/index.php?snippet=php-change-language-according-to-visitor-country
<?php
// prepare reload to local version according by first visit
session_start();
$location_reload = isset($_SESSION["loc_reload"]) ? (bool)$_SESSION["loc_reload"] : false;
// prepare reload to local version according by first visit
if(!$location_reload){
@apphp
apphp / directory_index
Created February 3, 2014 09:43
Different Directory Index Page in .htaccess
/*
We know that normally index.html or index.php is the default page for many servers, when visitor types a directory without specifying a file name. You can change this rule with .htaccess:
Source: http://www.apphp.com/index.php?snippet=htaccess-different-directory-index-page#null
*/
# Sample 1:
DirectoryIndex index2.html
# Sample 2:
DirectoryIndex index2.php
@apphp
apphp / gist:8781048
Created February 3, 2014 09:42
Remove File Extention from URLs in .htaccess
/*
This example shows you how to remove file extension from URLs using .htaccess file directives. Remember, that "mod_rewrite" works only on Apache server. Before trying please be sure that you are working on Apache server and the "mod_rewrite" module/extension is enabled.
Source: http://www.apphp.com/index.php?snippet=htaccess-remove-file-extention-from-url
*/
# Sample 1:
RewriteRule ^about$ about.php [L]
# Sample 2:
RewriteCond /%{REQUEST_FILENAME}.php -f
@apphp
apphp / gist:8765344
Created February 2, 2014 09:34
Create a Database and Assign a User
/*
This is a very simple snippet about how to create a database 1st,
a user and then assign some privileges to the user to allow him/her to
perform some specific actions like insert, create, update, select etc.
Source: http://www.apphp.com/index.php?snippet=mysql-create-database-and-assign-user
*/
-- Create database, user and grant all privileges
CREATE DATABASE database_name;
CREATE USER 'user_name'@'localhost' IDENTIFIED BY 'password';