Defvayne23

 

Link Wednesday

  • Project52 – The reason I started blogging. I have said I was going to for awhile, but this got me off the soap box and actually do it.
  • kuler – A site by Adobe. Good place to get color scheme ideas.
  • TiltShift Generator – Great online tilt shift tool. Also made for the iPhone.
  • Share/Bookmark

 

Saturday Mini Tip: Memcache flush

Memcache can be great to store variables, but beware of Memcache::flush. The function actually requires about 1 second to finish. Below is the best way to handle it:

1
2
3
4
5
6
7
 $oMemcache->flush();
 
// Wait for memcache to finish flushing
$time = time()+1; //one second future
while(time() < $time) {
//sleep
}

You can find more info here.

  • Share/Bookmark

 

TCT: Crawl a Website

DISCLAIMER: I would like to say I do not condone doing this. Better ways, more legal, ways to get content from someone. But sometimes this is asked of you by your boss. DO NOT STEAL CONTENT.

For this weeks Thursday Code Tip I will show how to use PHP to crawl a website to gather content. First we start by selecting the URL to crawl:

1
$sURL = "http://www.defvayne23.com/";

Next we get the content of the page:

2
$sContent = file_get_contents($sURL);

Now to use REGEX to get what we want. You can learn patterns here. Below we search for the text within a H1 tag.

3
4
$sPattern = '/<h1>([a-z0-9\s]+)<\/h1>/i';
preg_match($sPattern, $sContent, $aMatches);

The above won’t return anything because I link all my H1′s. So lets modify it so it will account for the links, but not gather them.

3
$sPattern = '/<h1><a [^>]+>([a-z0-9\s]+)<\/a><\/h1>/i';

Now that we account for the anchor the above should return:

1
2
3
4
5
Array
(
    [0] => Defvayne23
    [1] => Defvayne23
)

The first part of the array is the HTML it found including the h1 and anchor. The second is just the text that we where looking for.

Here it is all together:

1
2
3
4
5
$sURL = "http://www.defvayne23.com/";
$sContent = file_get_contents($sURL);
$sPattern = '/<h1><a [^>]+>([a-z0-9\s]+)<\/a><\/h1>/i';
preg_match($sPattern, $sContent, $aMatches);
$sHeader = $aMatches[1];
  • Share/Bookmark

 

Link Wednesday

  • Uncrate – The Buyer’s Guide For Men
    A blog dedicated to what men want. Anything from electronics, cars, clothes, movies, and cooking.
  • jsFiddle – Online editor for the web
    jsFiddle is like services Pastie and Gist.Github, but will run live test of HTML, CSS, and JavaScript. Includes frameworks such as jQuery, MooTools, Prototype, and others. Great for that idea that comes to mind or helping out a fellow developer.
  • PHPCamp – Learn something new everyday
    A gathering point for great articles and tutorials for PHP, CSS, and JavaScript. You can also follow PHPCamp on Twitter.
  • Clients From Hell
    A great collection of actual interactions with customers. Love seeing the ones I can relate to.
  • Glyphish – iPhone icon collection
    Free collection of icons to help style your iPhone application. Currently has 130 icons usable on both toolbars and tab bars.
  • Mac Endeavor – Mac Applications
    Mac Endeavor have created 3 awesome apps for the Mac. From a Git manager to window management that saves an apps window layout.
  • Share/Bookmark

 

Thursday Code Tip: Understand Classes

Working with classes in PHP is not only great for keeping your framework clean, but handling an item with ease. The makeImage helper for Kaizen CMS makes use of this. It’s easy to make changes to the image after you create the class object as below. Below code re-sizes an image, then crops to fit within an image restraint.

1
2
3
4
5
6
7
8
// Pass the file path and option to cache the image to the class
$oImage = new makeImage($sFile, true);
// Tell the object to resize the image, no need to pass the image
$oImage->resize(200, 100);
// Now crop the image after it was resized
$oImage->crop(100, 100, 0, 0);
// Now send the image data to the user
$oImage->draw(null, 100);

Class handled all the dirty work of keeping up with the image, and knowing all the info about the image. Above code is easier to maintain then doing all the code straight in the file. Class used above is available here.

  • Share/Bookmark