07/08/2013

Update a MySQL column trimming a constant number of characters

Will remove the first 4 characters. E.g. ABCD1234, ABCD1235 -> 1234, 1235:

UPDATE table_name
SET title = RIGHT(column_name, CHAR_LENGTH(column_name) - 4)
WHERE clause;

Advertisements
17/06/2013

Display your Twitter feed from a website

I have put up several posts on how to display recent Tweets in a webpage with PHP. However, Twitter has switched to it’s API v1.1 from today, so all of my previous articles have become obsolete. The easiest thing to do is to use Twitters own widget: https://twitter.com/settings/widgets. If you log in with your Twitter account, there is a simple wizard that generates some html that can be pasted directly into your webpage.

25/04/2013

Identify a (web) link in a string with PHP

If you want to process some text and output the same text with HTML links automatically created for you, then look no further:

$newstring = preg_replace('!(http|ftp|scp)(s)?:\/\/[a-zA-Z0-9.?&_/]+!', "<a href=\"\\" target='_blank'>\</a>",$sting);

Tags:
22/02/2013

UK to MySQL Date in C#

A C# snippet that will allow a function to be written where a string is passed in, containing a date in the format dd/mm/yyyy, and have that replaced by yyyy-mm-dd:

// Matches 2 digits forward slash 2 digits forward slash 4 digits e.g. 22/02/2013
Match m = Regex.Match(input, "\\d{2}/\\d{2}/\\d{4}");
// could use @"\d{2}/\d{2}/\d{4}" instead of the double backslash escape
if (m.Index > 0)
{
    string dd = input.Substring(m.Index, 2);
    string mm = input.Substring(m.Index + 3, 2);
    string yyyy = input.Substring(m.Index + 6, 4);

    input = input.Replace(dd + "/" + mm + "/" + yyyy, yyyy + "-" + mm + "-" + dd);
}

20/02/2013

Displaying your latest Tweet in PHP – updated

Back in 2011 I posted a method for displaying your latest tweet in php. However times change, and the twitter API has been changed beyond all recognition. I have updated my script to interpret the JSON response from the latest iteration of the API:

$username = "cytedesign";
$feed = "https://api.twitter.com/1/statuses/user_timeline/" . $username . ".json?count=1&include_rts=1&callback=?";

function parse_feed($feed) {
$stepOne = explode('"text":"', $feed);
$stepTwo = explode('","source"', $stepOne[1]);
$tweet = $stepTwo[0];
$tweet = htmlspecialchars_decode($tweet,ENT_QUOTES);
$tweet = str_replace("\/", "/", $tweet);
return $tweet;
}

$twitterFeed = file_get_contents($feed);
echo('&quot;'.parse_feed($twitterFeed).'&quot;');

I have been very busy with a brand new job that has been keeping me very busy, with more of a focus on developing desktop applications. I intend to start blogging a little again here as I get more comfortable with my new job.

Tags: , ,
23/07/2012

Notes on the Raspberry Pi

OS used: debian6-19-04-2012

I wanted to be able to play with my Pi, but allow others in my household to watch TV, so I picked up a 3.5″ TFT monitor from ebay for under £15, to make use of the RCA video output.

Videos on youtube I have seen with similar monitors all seem to suffer from not being able to read the text very clearly. I was able to adjust the font size on boot by editing /etc/default/console-setup  to the largest I could find: Lat15-Terminus32x16.psf.gz.

I also found that I needed to create a /boot/config.text file containing the line: overscan_left=20, to allow me to see the left edge of the display.

Hello World in C

Create hello.c in the directory of your choice:

#include <stdio.h>
main ()
{
  printf("Hello world\n");
}

I used the joe text editor to create the file.

Then run:

make hello

and you should see the output:

cc hello.c -o hello

finally you can run the program:

./hello

and get:

Hello world

Success!

Now onto using the GPIO… I have been reading up on WiringPi, so hopefully I will be able to extract some ideas from there.

Tags: , ,
15/10/2011

Set date and time of a Linux device from a Windows batchfile

I had a client with Linux device that whose clock is not as reliable as it should be, running a time-critical application.

As a workaround fix I wrote a Windows batchfile to update the Linux date and time, and  he has set up a windows scheduled task to run it every morning:

set Day=%date:~0,2%
set Month=%date:~3,2%
set Year=%date:~6,4%

set Hour=%time:~0,2%
set Minute=%time:~3,2%
set Second=%time:~6,2%

@C:\path_to_plink\plink.exe -ssh -pw linux_password linux_user@192.168.0.1 date %Month%%Day%%Hour%%Minute%%Year%.%Second%

Once again I have utilised one of the free PuTTY tools (plink.exe): http://www.chiark.greenend.org.uk/~sgtatham/putty/

05/10/2011

Force a favicon onto a webpage

<link rel="shortcut icon" href="http://www.my-domain.com/myfavicon.ico" type="image/x-icon" />


This will force a favicon onto your webpage – useful if you house different websites in subfolders – each subfolder site (or page for that matter) can have a different favicon.

Tags: ,
25/06/2011

More mobile browser detection testing

My standard method of mobile browser detection as detailed in my previous post here involves the PHP function preg_match(). Unfortunately for me, having compiled a stripped down version PHP to run on ARM linux, the preg_match() function was not included (probably because I used the disable-all switch during compilation).

Anyhow I was quite quickly able to come up with a workaround using strpos():

//Read the user agent string, and convert to lowercase
$useragent=strtolower($_SERVER['HTTP_USER_AGENT']);


//Array of mobile browsers
$mobile_browser=array('android','avantgo','blackberry','blazer','compal','elaine','fennec','hiptop','iemobile','iphone','ipod','iris','kindle','lge ','maemo','midp','mmp','opera mobi','opera mini','palm os','phone','pixi','pre','plucker','pocket','psp','symbian','treo','vodafone','wap','windows ce','windows phone','xda','xiino/i');


//initialise counter
$i=0;


//iterate thorough array
foreach($mobile_browser as $val) {
$pos = strpos($useragent, $val);
//if the array element is not in the user agent string, do nothing
if($pos === false) {
}
//else increment the counter
else {
$i++;
}
}
if ($i == 0) {
echo "Load normal site";
}
else {
echo "Load mobile site";
}

I also found a great way to test all mobile browsers, using the user agent switcher for Firefox.