Trim Text to a Certain Length on a Word Boundary in PHP

$caption = $your_starting_text;
if (strlen( $caption ) > 230) {  
     print preg_replace('/\s+?(\S+)?$/', '', substr( $caption , 0, 230)) . '...';
} else {
     print $caption ; 

Firebug Extension for AJAX Development

FirePHP enables you to log to your Firebug Console using a simple PHP method call. All data is sent via response headers and will not interfere with the content on your page.

FirePHP is ideally suited for AJAX development where clean JSON and XML responses are required.

Post to Facebook Wall/Timeline with the Facebook PHP SDK

 require_once '../facebook/facebook_sdk/src/facebook.php';
// configuration
 $appid = [APP ID];
 $appsecret = [SECRET APP KEY];
 $msg = 'Some message would go here.';
 $title = 'Some title would go here';
 $uri = '';
 $desc = 'Some detailed message that shows beneath the title.

HTTP POST to a REST Service through PHP

Representational state transfer (REST) is a style of software architecture for distributed systems such as the World Wide Web. The term representational state transfer was introduced and defined in 2000 by Roy Fielding in his doctoral dissertation.

Configure LAMP Server for Drupal 7 and Migrate Development Site - No GUI, command line only

To start with, I created a tarball with bz2 compression of the dev site. Make sure to explicitly include .htaccess because the tar command, like the ls command does not include filenames with "." as the first character. Navigate to the root of your site and execute the following (-c create | -v verbose (lists files as it goes) | -j bz2 compression | -f file):

tar -cvjf nameofsite_date_of_copy.tar.bz2 * .htaccess

Also, you will need the mysqldump

PHP Get Current URL

Sometimes it is not as straightforward as one may think to get the current url to use it inside your application. Here is a snippet that I use to fetch the current URL and use it in a script. The current url (whether http or https) is now a local variable that you can do with as you please.

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] :

You should groom this value before using in anything sensitive, like a sql query.

Mobile Device Detection in PHP

function mobile_device_detect($iphone=true,$ipad=true,$android=true,$opera=true,$blackberry=true,
  $mobile_browser   = false; 
// set mobile browser as false till we can prove otherwise
  $user_agent       = $_SERVER['HTTP_USER_AGENT']; 
// get the user agent value - this should be cleaned to ensure no nefarious input gets executed
  $accept           = $_SERVER['HTTP_ACCEPT']; 
// get the content accept value - this should be cleaned to ensure no nefarious input gets executed

Read a CSV in PHP line by line and access each field

$file_handle = fopen("widgets.csv", "r");
while (!feof($file_handle) ) {
$line_of_text = fgetcsv($file_handle, 1024);
print $line_of_text[0] . $line_of_text[1]. $line_of_text[2] . "<BR>";

Of course, you would increase the index of $line_of_text[i] if you had more than 3 columns.

Syndicate content