Category: PHP

PHP: Erase a value out of an array without knowing its key

By , December 9, 2013 12:07 pm

If you find yourself with an array that you know contains a certain value that you need to access or erase here is a useful code snippet that I came across the other day.

  1.  
  2. $key = array_search( $needle, $haystack );
  3. unset( $haystack[$key] );
  4.  

For example, given the following array:

  1.  
  2. $colors = array( 'red', 'blue', 'green', 'yellow' );
  3.  

If you wanted to get rid of 'green' but are unsure of its key:

  1.  
  2. $key = array_search( 'green', $colors );
  3. unset( $colors[$key] );
  4.  

Or you could change it to 'brown' with:

  1.  
  2. $key = array_search( 'green', $colors );
  3. $colors[$key] = 'brown';
  4.  

How to retrieve a usable date format from the Telligent REST API

By , April 23, 2013 8:06 am

Telligent's Community Server has a REST API that allows developers to tap into almost every aspect of the database. While creating an importer for WordPress I was attempting to backfill posts with the correct publish date. Telligent provides the publish date through a field called PublishedDate, however in its default format the date is not very useful. The default date from the API looks similar to:

/Date(1273255122713-0400)/

(Yes, including the slashes!)

WTF is that???

After spending a couple hours wrangling the data I finally got it to a place where it was close to the correct post time, however it was always off by a couple hours. This caused issues when rewriting inter-post links as the day of the post was sometimes incorrect, thus causing the year/month/day permalink to be invalid.

Then I found a hidden API parameter that solved all my issues...

Add UseIsoDateFormat=true to the the URL parameters and the following ISO date format will be returned:

yyyy-mm-ddThh:mm:ss

From there it is easy peazy to convert it into any format you may need.

Happy hacking!

First Experiences with WebMatrix 2 and Windows Azure Website

By , February 16, 2013 6:23 pm

WebMatrix is a free web development tool produced by Microsoft. WebMatrix is a complete development environment that not only provides a code editor, it also creates a new private instance of IIS Express to debug your project in, and downloads any dependencies your project may need, I.E. SQL Server Express, MySQL, PHP, etc.

I was asked what I thought of WebMatrix, however I have not spent a lot of time with it. I was working on a project that utilizes Windows Azure Web Sites and the App Gallery install of phpBB with another fellow. He was handling all the coding while I was administering the site through the web interface. I am now also coding for the site so I figured this is the perfect time to take WebMatrix for a spin..

Right off the bat I can tell you that if you are using Windows Azure Web Sites WebMatrix is a nice go to tool. WebMatrix is integrated with the Windows Azure Portal. Select your site from the Portal and there is a "WEBMATRIX" button on the toolbar. I did not have WebMatrix installed prior to starting this process. One-click of the button and WebMatrix was installed for me via Web Platform Installer.

Windows Azure Portal toolbar

Clicking the “WEBMATRIX” button automatically downloads the publish settings and sets up the WebMatrix environment for your project. Because WebMatrix understands phpBB it even pulled the PHP and SQL Server dependencies! The files and database were copied to my local machine. I am now able to interact with the phpBB web site through WebMatrix as an exact replica of the production website.

WebMatrix 2

WebMatrix even has a nifty tool that alerts you if you attempt to make changes to project core files.

WebMatrix 2 Alert

I made a few changes and clicked Run. My default browser opened and I was able to poke around the website as if it was running live. Satisfied that all seemed to be in working order, I clicked the Publish button a few minutes later. WebMatrix did a comparison of the files on the server to the local files to let me know which had changed. I could selectively choose which files to publish and which to retain. Even cooler, WebMatrix has an option to push back the entire database.

WebMatrix also supports code insight, or “Intellisense”, which is handy for finding class and function documentation throughout a project.

What is my verdict on WebMatrix? It seems to have matured into a great tool that is highly useful for web developers. Using Windows Azure Web Sites, or the WebMatrix Gallery you can, with a few clicks, have one of many popular open source content management systems (such as WordPress, Drupal, Joomla!) setup and running. The Publish feature eliminates the need for any additional applications to send files to the server. Additionally the automatic check of the server for changed files is very handy at ensuring only the proper files get uploaded or changed. Drawbacks? It currently only supports the Windows operating system.

Moving forwards I have a feeling WebMatrix will become my web code editor of choice, especially when working on Windows Azure related projects.

Count lines of code in files recursively

By , January 26, 2013 9:55 pm

If you are attempting to find a count of the total lines of code in files in a directory recursively from Linux or Mac OS X the following command will do what you want. This particular command prints out all lines contained in any file with a .php extension, then counts the number of lines printed.

  1. ( find . -name '*.php' -print0 | xargs -0 cat ) | wc -l

PHP: Find a file within a given path

By , December 12, 2012 8:23 pm

If you need to locate a file somewhere within a given path this code snipped will do it for you. Please note that it only works on files systems that can handle the Linux style architecture. This means that the code will likely not work on a Windows Server.

I created this snippet specifically to locate the WordPress wp-load.php file in the path, however it should be noted that I do not necessarily advocate this method, it simply worked and was quick to implement. I am posting it here for posterity. Perhaps someone can benefit from it.

Please post a comment if you know of a better solution.

Problem: wp-load.php is somewhere in the path but it is unknown where. wp-load.php needs to be included from a file that may potentially move around.

Solution:

  1. /**
  2.  * Searches for a file in the path upwards from the provided $path
  3.  *
  4.  * NOTE: This *ONLY* works with Linux based path structures. I.E. Will likely
  5.  * not work with any Windows server
  6.  *
  7.  * @param String $file - Name of file to find
  8.  * @param String $path - (Optional) Path to begin searching from. Defaults to current
  9.  * @return Mixed - Full path to file or null if not found
  10.  */
  11. function find_in_path( $file, $path = null ) {
  12.  
  13. if( is_null( $path ) ) $path = getcwd();
  14.  
  15. if( file_exists( "$path/$file" ) ) {
  16. return "$path/$file";
  17. } else {
  18. $path = explode( '/', $path );
  19.  
  20. if( '' == $path['1'] )
  21. return null;
  22.  
  23. unset( $path['0'] );
  24. unset( $path[ count( $path )] );
  25.  
  26. return find_in_path( $file, '/' .implode( '/', $path ) );
  27.  
  28. }
  29. }

Usage:

  1. equire_once( find_in_path( 'wp-load.php' ) );

Theme by Blam Designs
Based on Themocracy

Add this ribbon to your WordPress website re-abolish slavery