Skip to content

Instantly share code, notes, and snippets.

View jwv's full-sized avatar

Jonathan Vicente jwv

View GitHub Profile
@jwv
jwv / increment_field.sql
Created June 5, 2012 00:02
Mysql: Increment field
set @i = 0;
UPDATE table_name SET field = (@i:=@i+1)
@jwv
jwv / QRCode_from_url.php
Created November 26, 2012 14:58
PHP: Generate QRCode From Url
function generateQRCodeFromUrl($your_url, $width, $height, $file) {
$your_url = urlencode($your_url);
$url = 'http://chart.apis.google.com/chart?chs='.$width.'x'.$height.'&cht=qr&chl='.$your_url;
// initialize cURL settings
$ch = curl_init ($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
@jwv
jwv / slug.php
Created November 30, 2012 15:08
PHP: Slug
function to_slug($string){
return strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
}
@jwv
jwv / split_text.php
Created December 4, 2012 09:34
PHP: split text
function splitText($text, $maxLength)
{
/* Make sure that the string will not be longer
than $maxLength.
*/
if(strlen($text) > $maxLength)
{
/* Trim the text to $maxLength characters */
$text = substr($text, 0, $maxLength - 1);
@jwv
jwv / splitTextByWords.php
Created December 4, 2012 09:35
PHP: split text by words
function splitTextByWords($str, $words = 10)
{
$arr = preg_split("/[\s]+/", $str, $words+1);
$arr = array_slice($arr, 0, $words);
return join(' ', $arr);
}
@jwv
jwv / jq_animate_numbers.js
Created December 9, 2012 05:01
jQuery: Animate Numbers
jQuery({n: 0}).animate({n: 110}, {
duration: 1000,
easing:'swing', // can be anything
step: function() { // called on every step
// Update the element's text with rounded-up value:
$('#el').text(Math.ceil(this.n) + "%");
}
});
function crawlerDetect($user_agent)
{
$crawlers_agents = 'Google|msnbot|Rambler|Yahoo|AbachoBOT|accoona|AcioRobot|ASPSeek|CocoCrawler|Dumbot|FAST-WebCrawler|GeonaBot|Gigabot|Lycos|MSRBOT|Scooter|AltaVista|IDBot|eStyle|Scrubby';
return strpos($crawlers_agents , $USER_AGENT) === false)?0:1;
}
@jwv
jwv / Highlight_Words_In_A_String.php
Created December 18, 2012 14:29
PHP: Highlight Words In A String
<?php
function highlight($astring, $aword) {
if (!is_array ($aword) || !is_string ($astring) || empty ($aword) ) {
return false;
}
$aword = implode ('|', $aword);
return preg_replace ('@\b('.$aword.')\b@si', '<strong style="background-color:red">$1</strong>', $astring);
}
@jwv
jwv / calc_distances.php
Created January 8, 2013 02:25
PHP: Calculate distances
function distance($lat1, $lon1, $lat2, $lon2, $unit) {
$theta = $lon1 - $lon2;
$dist = sin(deg2rad($lat1)) * sin(deg2rad($lat2)) + cos(deg2rad($lat1)) * cos(deg2rad($lat2)) * cos(deg2rad($theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
$unit = strtoupper($unit);
if ($unit == "K") {
@jwv
jwv / force_download.php
Created January 8, 2013 02:26
PHP: Force download
function downloadFile($file){
$file_name = $file;
$mime = 'application/force-download';
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type: '.$mime);
header('Content-Disposition: attachment; filename="'.basename($file_name).'"');
header('Content-Transfer-Encoding: binary');