Tag Archive for from

Function to convert dates to and from Mysql

<?php

/**
* function dateconvert
*
* dateconvert is a handy function to take the aches and pains out of mysqls stupidity
* by converting data from a variable (posted from a form or just stored)
* into a format mysql will be able to store and converting the
* database date back into the british standard of date month year.
* The Script accepts day.month.year or day/month/year or day-month-year.
* example:
*
* <code>
* <?php // using type 1
* $date = "19.12.2005";
* $date = dateconvert($date, 1);
* echo $date; // Would echo 2005-12-19 which is the format stored by mysql
* ?>
* </code>
* <code>
* <?php // using type 2
* $date = $row['date']; //your mysql date
* $realdate = dateconvert($date,2);
* echo $realdate; // would display 19/12/2005
* ?>
* </code>
*
* @author Chris McKee <<a href="mailto:pcdevils@gmail.com">pcdevils@gmail.com</a>>
*
* @param string $date - Date to be converted
* @param string $func - which function is to be used (1 for input to mysql, 2 for output from mysql)
*/
function dateconvert($date,$func) {
if ($func == 1){ //insert conversion
list($day, $month, $year) = split('[/.-]', $date);
$date = "$year-$month-$day";
return $date;
}
if ($func == 2){ //output conversion
list($year, $month, $day) = split('[-.]', $date);
$date = "$day/$month/$year";
return $date;
}

}

?>

source

PHP Create Google Map Directions Link

function googleMapLink($to, $from) {
$destinationAddy = '&daddr='.urlencode($to->getAddress().' '.$to->getCity().' '.$to->getState());

if($from) {
$startAddy = '&saddr='.urlencode($from->getAddress().' '.$from->getCity().' '.$from->getState());
} else {
$startAddy = '&saddr=';
}

return htmlentities("http://maps.google.com/maps?f=q&amp;hl=en&amp;{$startAddy}{$destinationAddy}");
}

source

Prevent Users From Right Clicking Images

<img src="imageurl.jpg" alt="" oncontextmenu="return false;" />

source

Import data from tab – delimited datafile to SQL table.

> LOAD DATA LOCAL INFILE '/tmp/testdata.txt' INTO TABLE testtable FIELDS TERMINATED BY '	';

> LOAD DATA LOCAL INFILE '/tmp/VICIDB/matltran.txt' INTO TABLE matltran FIELDS TERMINATED BY '	' FIELDS OPTIONALLY ENCLOSED BY '"';

source

Download file from URL

<?
// DOWNLOAD REMOTE FILE
// this is a-lot more complicated than it looks
// $url is the url of the file, must have fopen wrappers enabled
// $dir is the directory to save the file to relative to the current working directory
// file is saved with the same file name from its source
function getfile($url, $dir){
file_put_contents($dir.substr($url,strrpos($url,'/'),strlen($url)), file_get_contents($url));
}
?>

source

Remove Common Words

function removeCommonWords($input){

// EEEEEEK Stop words
$commonWords = array('a','able','about','above','abroad','according','accordingly','across','actually','adj','after','afterwards','again','against','ago','ahead','ain't','all','allow','allows','almost','alone','along','alongside','already','also','although','always','am','amid','amidst','among','amongst','an','and','another','any','anybody','anyhow','anyone','anything','anyway','anyways','anywhere','apart','appear','appreciate','appropriate','are','aren't','around','as','a's','aside','ask','asking','associated','at','available','away','awfully','b','back','backward','backwards','be','became','because','become','becomes','becoming','been','before','beforehand','begin','behind','being','believe','below','beside','besides','best','better','between','beyond','both','brief','but','by','c','came','can','cannot','cant','can't','caption','cause','causes','certain','certainly','changes','clearly','c'mon','co','co.','com','come','comes','concerning','consequently','consider','considering','contain','containing','contains','corresponding','could','couldn't','course','c's','currently','d','dare','daren't','definitely','described','despite','did','didn't','different','directly','do','does','doesn't','doing','done','don't','down','downwards','during','e','each','edu','eg','eight','eighty','either','else','elsewhere','end','ending','enough','entirely','especially','et','etc','even','ever','evermore','every','everybody','everyone','everything','everywhere','ex','exactly','example','except','f','fairly','far','farther','few','fewer','fifth','first','five','followed','following','follows','for','forever','former','formerly','forth','forward','found','four','from','further','furthermore','g','get','gets','getting','given','gives','go','goes','going','gone','got','gotten','greetings','h','had','hadn't','half','happens','hardly','has','hasn't','have','haven't','having','he','he'd','he'll','hello','help','hence','her','here','hereafter','hereby','herein','here's','hereupon','hers','herself','he's','hi','him','himself','his','hither','hopefully','how','howbeit','however','hundred','i','i'd','ie','if','ignored','i'll','i'm','immediate','in','inasmuch','inc','inc.','indeed','indicate','indicated','indicates','inner','inside','insofar','instead','into','inward','is','isn't','it','it'd','it'll','its','it's','itself','i've','j','just','k','keep','keeps','kept','know','known','knows','l','last','lately','later','latter','latterly','least','less','lest','let','let's','like','liked','likely','likewise','little','look','looking','looks','low','lower','ltd','m','made','mainly','make','makes','many','may','maybe','mayn't','me','mean','meantime','meanwhile','merely','might','mightn't','mine','minus','miss','more','moreover','most','mostly','mr','mrs','much','must','mustn't','my','myself','n','name','namely','nd','near','nearly','necessary','need','needn't','needs','neither','never','neverf','neverless','nevertheless','new','next','nine','ninety','no','nobody','non','none','nonetheless','noone','no-one','nor','normally','not','nothing','notwithstanding','novel','now','nowhere','o','obviously','of','off','often','oh','ok','okay','old','on','once','one','ones','one's','only','onto','opposite','or','other','others','otherwise','ought','oughtn't','our','ours','ourselves','out','outside','over','overall','own','p','particular','particularly','past','per','perhaps','placed','please','plus','possible','presumably','probably','provided','provides','q','que','quite','qv','r','rather','rd','re','really','reasonably','recent','recently','regarding','regardless','regards','relatively','respectively','right','round','s','said','same','saw','say','saying','says','second','secondly','see','seeing','seem','seemed','seeming','seems','seen','self','selves','sensible','sent','serious','seriously','seven','several','shall','shan't','she','she'd','she'll','she's','should','shouldn't','since','six','so','some','somebody','someday','somehow','someone','something','sometime','sometimes','somewhat','somewhere','soon','sorry','specified','specify','specifying','still','sub','such','sup','sure','t','take','taken','taking','tell','tends','th','than','thank','thanks','thanx','that','that'll','thats','that's','that've','the','their','theirs','them','themselves','then','thence','there','thereafter','thereby','there'd','therefore','therein','there'll','there're','theres','there's','thereupon','there've','these','they','they'd','they'll','they're','they've','thing','things','think','third','thirty','this','thorough','thoroughly','those','though','three','through','throughout','thru','thus','till','to','together','too','took','toward','towards','tried','tries','truly','try','trying','t's','twice','two','u','un','under','underneath','undoing','unfortunately','unless','unlike','unlikely','until','unto','up','upon','upwards','us','use','used','useful','uses','using','usually','v','value','various','versus','very','via','viz','vs','w','want','wants','was','wasn't','way','we','we'd','welcome','well','we'll','went','were','we're','weren't','we've','what','whatever','what'll','what's','what've','when','whence','whenever','where','whereafter','whereas','whereby','wherein','where's','whereupon','wherever','whether','which','whichever','while','whilst','whither','who','who'd','whoever','whole','who'll','whom','whomever','who's','whose','why','will','willing','wish','with','within','without','wonder','won't','would','wouldn't','x','y','yes','yet','you','you'd','you'll','your','you're','yours','yourself','yourselves','you've','z','zero');

return preg_replace('/('.implode('|',$commonWords).')/','',$input);
}

source

Get All Cookies From HTTP Header

public static CookieCollection GetAllCookiesFromHeader(string strHeader, string strHost)
{
ArrayList al = new ArrayList();
CookieCollection cc = new CookieCollection();
if (strHeader != string.Empty)
{
al = ConvertCookieHeaderToArrayList(strHeader);
cc = ConvertCookieArraysToCookieCollection(al, strHost);
}
return cc;
}


private static ArrayList ConvertCookieHeaderToArrayList(string strCookHeader)
{
strCookHeader = strCookHeader.Replace("
", "");
strCookHeader = strCookHeader.Replace("
", "");
string[] strCookTemp = strCookHeader.Split(',');
ArrayList al = new ArrayList();
int i = 0;
int n = strCookTemp.Length;
while (i < n)
{
if (strCookTemp[i].IndexOf("expires=", StringComparison.OrdinalIgnoreCase) > 0)
{
al.Add(strCookTemp[i] + "," + strCookTemp[i + 1]);
i = i + 1;
}
else
{
al.Add(strCookTemp[i]);
}
i = i + 1;
}
return al;
}


private static CookieCollection ConvertCookieArraysToCookieCollection(ArrayList al, string strHost)
{
CookieCollection cc = new CookieCollection();

int alcount = al.Count;
string strEachCook;
string[] strEachCookParts;
for (int i = 0; i < alcount; i++)
{
strEachCook = al[i].ToString();
strEachCookParts = strEachCook.Split(';');
int intEachCookPartsCount = strEachCookParts.Length;
string strCNameAndCValue = string.Empty;
string strPNameAndPValue = string.Empty;
string strDNameAndDValue = string.Empty;
string[] NameValuePairTemp;
Cookie cookTemp = new Cookie();

for (int j = 0; j < intEachCookPartsCount; j++)
{
if (j == 0)
{
strCNameAndCValue = strEachCookParts[j];
if (strCNameAndCValue != string.Empty)
{
int firstEqual = strCNameAndCValue.IndexOf("=");
string firstName = strCNameAndCValue.Substring(0, firstEqual);
string allValue = strCNameAndCValue.Substring(firstEqual + 1, strCNameAndCValue.Length - (firstEqual + 1));
cookTemp.Name = firstName;
cookTemp.Value = allValue;
}
continue;
}
if (strEachCookParts[j].IndexOf("path", StringComparison.OrdinalIgnoreCase) >= 0)
{
strPNameAndPValue = strEachCookParts[j];
if (strPNameAndPValue != string.Empty)
{
NameValuePairTemp = strPNameAndPValue.Split('=');
if (NameValuePairTemp[1] != string.Empty)
{
cookTemp.Path = NameValuePairTemp[1];
}
else
{
cookTemp.Path = "/";
}
}
continue;
}

if (strEachCookParts[j].IndexOf("domain", StringComparison.OrdinalIgnoreCase) >= 0)
{
strPNameAndPValue = strEachCookParts[j];
if (strPNameAndPValue != string.Empty)
{
NameValuePairTemp = strPNameAndPValue.Split('=');

if (NameValuePairTemp[1] != string.Empty)
{
cookTemp.Domain = NameValuePairTemp[1];
}
else
{
cookTemp.Domain = strHost;
}
}
continue;
}
}

if (cookTemp.Path == string.Empty)
{
cookTemp.Path = "/";
}
if (cookTemp.Domain == string.Empty)
{
cookTemp.Domain = strHost;
}
cc.Add(cookTemp);
}
return cc;
}

source

Convert XmlNodeList To DataTable

public static DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
{
DataTable dt = new DataTable();
int TempColumn = 0;

foreach (XmlNode node in xnl.Item(0).ChildNodes)
{
TempColumn++;
DataColumn dc = new DataColumn(node.Name, System.Type.GetType("System.String"));
if (dt.Columns.Contains(node.Name))
{
dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());
}
else
{
dt.Columns.Add(dc);
}
}

int ColumnsCount = dt.Columns.Count;
for (int i = 0; i < xnl.Count; i++)
{
DataRow dr = dt.NewRow();
for (int j = 0; j < ColumnsCount; j++)
{
dr[j] = xnl.Item(i).ChildNodes[j].InnerText;
}
dt.Rows.Add(dr);
}
return dt;
}

source

PHP System Load on Mac OS X

#!/usr/bin/php
<?php

// This works with Mac OS X `uptime`
// others might require tweaks

error_reporting( E_ALL );

function load()
{
$array = explode( ',', trim( `uptime` ) );

while( list( , $value ) = each( $array ) )
{
if( eregi( 'load', $value ) )
{
$parts = explode( ':', trim( $value ) );

$loads = explode( ' ', $parts[ count( $parts ) - 1 ] );

$load = $loads[ 1 ];

break;
}
}

return $load;
}

echo load();

?>

source

display data from myusql

$query = 'SELECT field1, field2 FROM thisTable ORDER BY dateline LIMIT 5';
$result = mysql_query($query);
$items = array();
while ($row = mysql_fetch_assoc($result)) {
$items[] = sprintf('<li><a href="%s">%s</a></li>', $row['field1'], $row['field2']);
}
//do something with $items array;

source