Posted: May 5th, 2010 | Author: Henrie Media Inc. | Filed under: PHP | No Comments »
This is a useful script for anybody who wishes to get the beginning of a string up to the word break before $num of characters. Usually the use of substr will break half way through a word and leave things looking a little untidy on your site. This little function allows you to set the maximum length of the string, then it returns all the whole words before it. If the supplied string is shorter than the maximum length, the whole string is returned.
<?php
/**
*
* @break a string at the end of the last word
* before $maxlength chars
*
* @param string $string
*
* @param int $maxlength
*
* @return string
*
*/
function wordbreak($string, $maxlength)
{
$string = substr($string, 0, $maxlength);
return substr($string, 0, strrpos($string, ” ”));
}
/*** example usage ***/
$string = ’Kyle was hoping to be finished before he left on his vacation to Hawaii! He also wanted to make sure the dog was fed, the house was clean and the lawn was mowed’;
echo wordbreak($string, 50).’ …’;
?>
Print, Email, Share, Enjoy!
Posted: April 14th, 2010 | Author: Henrie Media Inc. | Filed under: Code Library, PHP | No Comments »
Many times it is neat to display the current temperature on your website, and there are a lot of different ways to go about this. I’d like to show you the easiest way to display the temperature on your site using XML from the National Weather Service (NOAA) and simple XML commands in PHP.
Get Started
To get started, you will need to find weather data. The National Weather Service offers this data in free XML feeds, which you can find at http://www.weather.gov/xml/current_obs/. To get started, select an XML feed you’d like to use. I’m using a Salt Lake City, Utah, ID feed, which looks like:
http://www.weather.gov/xml/current_obs/KSLC.xml
You will is this URL in the PHP code to get your location’s current temperature.
The Code
To get the data we want from the XML feed, we will be using the simplexml_load_file() function. This will, essentially, parse the XML for us, making it ready to use as text online. Here is the code you will need:
<?php
// Load the XML
$xml_slc = simplexml_load_file(‘http://www.weather.gov/xml/current_obs/KSLC.xml’);
// Get the current temperature
$slc_temp = $xml_slc->temp_f;
//echo $slc_temp.”° F”;
?>
The output you get should be something similar to 64°. It’s that easy and and it’s FREE! Enjoy!
Print, Email, Share, Enjoy!
Posted: February 23rd, 2009 | Author: Henrie Media Inc. | Filed under: Code Library, PHP | No Comments »
When developing PHP-driven websites its often very useful to know how long your pages took to process. This not only gives you some idea of the efficiency of your websites and of the server running them, but may help you diagnose problems, benchmark code corrections/additions, etc. The best way to do this is program PHP to read the system time at the beginning of the page, scan it again near the end, and then work out the difference between the values. To get us started, just add the following basic code near the top of your page, before all the main content:
<?php
$starttime = microtime();
$startarray = explode(” “, $starttime);
$starttime = $startarray[1] + $startarray[0];
?>
It may look complex, but the above code is actually dead simple. Microtime is a function that returns a value giving the current time in seconds and milliseconds from the UNIX Epoch (internationally set as 0:00:00 January 1, 1970 GMT). Explode then splits this value into its two component parts (seconds and milliseconds), and then inserts both values separately into an array. With those values determined, of course, we can go on to insert the following code near the end of the webpage, after all the main code:
<?php
$endtime = microtime();
$endarray = explode(” “, $endtime);
$endtime = $endarray[1] + $endarray[0];
$totaltime = $endtime – $starttime;
$totaltime = round($totaltime,5);
echo “This page loaded in $totaltime seconds.”;
?>
The microtime is determined again and inserted into the endtime array. We then delete the starttime array from it and come up with a brand new value, totaltime (which is the value we’ve wanted all along). The number is then rounded to 5 significant figures, and output by the ECHO command. If you’ve entered everything correctly, you should end up with a line of text saying something like this:
Page generated in 0.27283 seconds
That is all there is to it. Make sure you keep in mind that this measure depends heavily on server load levels, mySQL commands, etc. Make sure you take into account all of these factors when determine load time.
Print, Email, Share, Enjoy!