Speedtest on Ubuntu Server (commandline)

Using the speedtest-cli is easy and very useful. You run it as follows: 1 2 3 wget -O speedtest-cli https://raw.github.com/sivel/speedtest-cli/master/speedtest_cli.py chmod +x speedtest-cli ./speedtest-cli

URL GET vars to PHP Array

Sometimes you will need to retrieve the GET variables passed into the current page URI or you will have a URL string to work from which contains certain GET variables, the below method helps a lot to convert them into an array which you can easily manipulate later. 1 2 $url = $_SERVER["REQUEST_URI"]; parse_str(parse_url($url, PHP_URL_QUERY), $array); $array is now an array of all the GET variables in the URL. Alternatively you can pass a URI string in place of the $_SERVER[“REQUEST_URI”] by replacing the $url variable with something else.

python "AttributeError: ZipFile instance has no attribute ‘__exit__"

This is actually a very easy error to fix, eventhough off the bat it seems a lot more involved. You probably have syntax something like this: 1 2 with zipfile.ZipFile(wr_zip) as zipfd: extract(zipfd, wr_csv, wr_csv) So instead we will change it to this: 1 2 zipfd = zipfile.ZipFile(wr_zip) extract(zipfd, wr_csv, wr_csv) The reason this fixes it is because at the moment (Python 2.6/2.7 I believe) the zipfile.ZipFile class has no __exit__ attribute, so it does not work with a `with statement` as other file objects do.

Remove specific HTML tags using PHP

There are times when you want to remove a specific HTML tag from an HTML block of text. This could be an anchor(>) or an image() perhaps. You can use preg_replace to do this quite quickly and efficiently. Remove an anchor: 1 2 3 $content = "Sample text <a href="#">Our anchor</a>. Etc etc"; $content = preg_replace('/<\/?a[^>]*>/','',$content); //$content is now -> "Sample text. Etc etc"; Remove an image: 1 2 3 $content = "Sample text <img src="our_image.

Keep Google Map v3 centered when browser is resized

Using Google Maps V3 Javascript API you can keep the map centered to the browser’s window by using the following trick when resizing the window. 1 2 3 4 5 6 7 8 9 10 11 12 13 var mapOptions = { zoom: 2, center: new google.maps.LatLng(0,0), mapTypeId: google.maps.MapTypeId.ROADMAP }; var map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); // this is our gem google.maps.event.addDomListener(window, "resize", function() { var center = map.getCenter(); google.maps.event.trigger(map, "resize"); map.

Buy a Google Nexus 10 in the UK!

If you are looking to get a Google Nexus 10 inch tablet and are based in the UK you are in for a shocker! The only place to buy it is at the Google Play store and since about the middle of November 2012 they have been sold out. Some people say it’s due to delayed deliveries and orders and some say it’s just bad management on Google’s behalf. I think the latter is probably the thing that actually happened as no other tablet providers seem to ever have this problem.

Let Joomla and MySQL interact!

I often need a quick and easy few lines to retrieve some data from MySQL using Joomla without all the MVC nonsense that usually goes about this topic. So here is how I do it! 1 2 3 4 $dbSEL = JFactory::getDbo(); $dbSEL->setQuery("SELECT MAX(`start_price`) FROM #__some_table"); $dbSEL->query(); echo $dbSEL->fetchRow(); As you can see above, we get the current database object and then run a query on it. You may notice the hash (#) in the SQL statement of this example, Joomla replaces that (as well as the underscores (__) with the database prefix which is configured, so on shared nodes you don’t have to type all that extra stuff that will need to be changed later.

Perform a Mysql Query using Joomla!

This is really actually meant for future reference for myself as lately I’ve been doing quite a lot of Joomla! mysql coding and have used the following code just short of 3million times. 1 2 3 4 5 $db = JFactory::getDbo(); $querySelectId = "SELECT `id` FROM #__some_table WHERE `user_id`='$user_id'"; $db->setQuery($querySelectId); $db->query(); $data = $db->loadAssoc(); What we do in the above code snippet is select a user_id from some_table (#__) is short for whatever your database table’s prefix is (Joomla!

Refresh User Data in Joomla

I was busy with a custom component in Joomla, and it stored it’s own user_details based off of the main users table, but if the details were changed then I needed the system to update the session to reflect the changes. This is what I did: 1 2 3 4 $user = JFactory::getUser(); $session = JFactory::getSession(); $session->set("user", new JUser($user->id)); $user = JFactory::getUser();

Extract email addresses from a string – PHP

Sometimes you need to extract multiple email addresses from a string and the following function will make all your dreams come true. 1 2 3 4 function extract_emails_from($string){ preg_match_all("/[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i", $string, $matches); return $matches[0]; } ..and this is how you use it: 1 2 $text = "this is some text and here is an email address [email protected], here's another [email protected], etc.."; $emails = extract_emails_from($text); Now let’s use the data: 1 2 3 4 5 6 7 // as string print(implode("\n", $emails)); // loop as array foreach($emails as $email) { echo $email .

How to drive traffic to your website!

This is a very hot topic indeed. Definitely one that everyone with a website should know all about. To begin our journey we will look directly at the Traffic Generator Sites out there who – to be honest – are complete scams because all they really do is one of two things… 1.) Load your site into those annoying pop-under ad windows that users close immediately as they appear(not having opened them themselves)

jQuery limit fields to alphanumeric characters only

I was trying to restrict a form’s field from only allowing the user to enter alphanumeric characters into it. I looked all around and couldn’t find a nice way to do it without a plugin, so I wrote my own bind function that you can attach straight to an element. 1 2 3 4 5 6 7 8 9 10 $("#yourFieldsElementId").bind("keypress", function (event) { if (event.charCode!=0) { var regex = new RegExp("^[a-zA-Z0-9]+$"); var key = String.

Make a dynamic year dropdown using PHP

Ever wanted to have a dropdown that automatically showed the current year and the few years before it? This is a quick and easy way to do exactly that! 1 2 3 4 5 6 7 8 <select name="year"> <?php for($i=date("Y")-5;$i<=date("Y");$i++) { $sel = ($i == date('Y')) ? 'selected' : ''; echo "<option value=".$i." ".$sel."-->".date("Y", mktime(0,0,0,0,1,$i)).""; } ?> </select>

Pad a string with zeros using PHP

Recently I had to create a code/username maker to fit into a certain type of pattern so that all “broker codes” (as they were called) followed the same path. An example one looked like so: 1 HJTH0001 The number needed to increment and I wanted to use the auto-increment feature in the mysql database table that was driving this data to make it. This was easy but I ended up with a code that looked like this:

ImportError: No module named MySQLdb (Python)

Trying to use MySQL with Python and getting an ImportError? Traceback (most recent call last): File "some_file.py", line 4, in import MySQLdb as mdb ImportError: No module named MySQLdb This is likely because you have not installed it to the system. You can do this on Ubuntu Linux by running the following code: sudo apt-get install python-mysqldb Also, remember that there is no MySQLdb for python3.x and above. There is however, a fork of it on GitHub at: https://github.

Search for “arabic” is url request and change codepage – ASP

If you are using Classic ASP (yuck) to create an arabic section of a website you can search for the arabic string in your Request URI and then change the session’s codepage as follows: 1 2 3 if instr(1, request.ServerVariables("PATH_INFO"), "arabic") then session.codepage = 1252 end if

Windows 7 is better than Ubuntu 12.04 on desktop!

Today we have finalised for ourselves that Windows 7 is definitely a “better” operating system than Ubuntu 12.04 when it comes to desktop environments. Now, I say “better” in quotes because the word is quite hard to use as is and “convince” everybody while doing so. We started our journey on Windows 7 and after desperately needing a reinstall we decided to go for the latest version (at the time) of Ubuntu, which happened to be the brand spanking new Ubuntu 12.

Function split() is deprecated in PHP

You heard it right! split() is officially a deprecated function. That means that you can still use it if you are really brave and it will work correctly, but don’t expect to see it in later versions of PHP when they come out. Somewhere along the line it WILL not work at all. It has been marked as “deprecated” due to the fact that the explode() function has the exact same output!

Disable Cache in jQuery

I usually run into this problem when dealing with good ol’ Internet Explorer (..any version of IE actually). The problem is that IE tries to be smart and not tell you the new output of a file it fetches more than one time with the same filename, instead it shows you what it saw the first time it loaded that file. You can imagine this would be insanely dumb if you were using a realtime application data source where a script output a different resultset each time, e.

Where is Technology heading, perhaps we should focus more on Software?

I often wonder to myself where the technology industry is heading as I see new things come out almost daily. You look around and something you just bought is all of a sudden outdated. This can be quite irritating for the techno-geeks out there that always want the latest release of every gadget, as it makes it somewhat impossible to keep up with what’s in stores. The really nice thing about software over hardware is that one can just perform a localised update instead of having to purchase new hardware everytime something changes or is improved.