Monday, 3 February 2014

Create Dynamic Date Time Dropdown List

The menu is generated using PHP and all values default to current values for year, month, day, and hour. These values can be set along with the name and id of each element.

<?php
    
/**
    *
    * @Create dropdown of years
    *
    * @param int $start_year
    *
    * @param int $end_year
    *
    * @param string $id The name and id of the select object
    *
    * @param int $selected
    *
    * @return string
    *
    */
    
function createYears($start_year$end_year$id='year_select'$selected=null)
    {

        
/*** the current year ***/
        
$selected is_null($selected) ? date('Y') : $selected;

        
/*** range of years ***/
        
$r range($start_year$end_year);

        
/*** create the select ***/
        
$select '<select name="'.$id.'" id="'.$id.'">';
        foreach( 
$r as $year )
        {
            
$select .= "<option value=\"$year\"";
            
$select .= ($year==$selected) ? ' selected="selected"' '';
            
$select .= ">$year</option>\n";
        }
        
$select .= '</select>';
        return 
$select;
    }

    
/*
    *
    * @Create dropdown list of months
    *
    * @param string $id The name and id of the select object
    *
    * @param int $selected
    *
    * @return string
    *
    */
    
function createMonths($id='month_select'$selected=null)
    {
        
/*** array of months ***/
        
$months = array(
                
1=>'January',
                
2=>'February',
                
3=>'March',
                
4=>'April',
                
5=>'May',
                
6=>'June',
                
7=>'July',
                
8=>'August',
                
9=>'September',
                
10=>'October',
                
11=>'November',
                
12=>'December');

        
/*** current month ***/
        
$selected is_null($selected) ? date('m') : $selected;

        
$select '<select name="'.$id.'" id="'.$id.'">'."\n";
        foreach(
$months as $key=>$mon)
        {
            
$select .= "<option value=\"$key\"";
            
$select .= ($key==$selected) ? ' selected="selected"' '';
            
$select .= ">$mon</option>\n";
        }
        
$select .= '</select>';
        return 
$select;
    }


    
/**
    *
    * @Create dropdown list of days
    *
    * @param string $id The name and id of the select object
    *
    * @param int $selected
    *
    * @return string
    *
    */
    
function createDays($id='day_select'$selected=null)
    {
        
/*** range of days ***/
        
$r range(131);

        
/*** current day ***/
        
$selected is_null($selected) ? date('d') : $selected;

        
$select "<select name=\"$id\" id=\"$id\">\n";
        foreach (
$r as $day)
        {
            
$select .= "<option value=\"$day\"";
            
$select .= ($day==$selected) ? ' selected="selected"' '';
            
$select .= ">$day</option>\n";
        }
        
$select .= '</select>';
        return 
$select;
    }


    
/**
    *
    * @create dropdown list of hours
    *
    * @param string $id The name and id of the select object
    *
    * @param int $selected
    *
    * @return string
    *
    */
    
function createHours($id='hours_select'$selected=null)
    {
        
/*** range of hours ***/
        
$r range(112);

        
/*** current hour ***/
        
$selected is_null($selected) ? date('h') : $selected;

        
$select "<select name=\"$id\" id=\"$id\">\n";
        foreach (
$r as $hour)
        {
            
$select .= "<option value=\"$hour\"";
            
$select .= ($hour==$selected) ? ' selected="selected"' '';
            
$select .= ">$hour</option>\n";
        }
        
$select .= '</select>';
        return 
$select;
    }

    
/**
    *
    * @create dropdown list of minutes
    *
    * @param string $id The name and id of the select object
    *
    * @param int $selected
    *
    * @return string
    *
    */
    
function createMinutes($id='minute_select'$selected=null)
    {
        
/*** array of mins ***/
        
$minutes = array(0153045);

    
$selected in_array($selected$minutes) ? $selected 0;

        
$select "<select name=\"$id\" id=\"$id\">\n";
        foreach(
$minutes as $min)
        {
            
$select .= "<option value=\"$min\"";
            
$select .= ($min==$selected) ? ' selected="selected"' '';
            
$select .= ">".str_pad($min2'0')."</option>\n";
        }
        
$select .= '</select>';
        return 
$select;
    }

    
/**
    *
    * @create a dropdown list of AM or PM
    *
    * @param string $id The name and id of the select object
    *
    * @param string $selected
    *
    * @return string
    *
    */
    
function createAmPm($id='select_ampm'$selected=null)
    {
        
$r = array('AM''PM');

    
/*** set the select minute ***/
        
$selected is_null($selected) ? date('A') : strtoupper($selected);

        
$select "<select name=\"$id\" id=\"$id\">\n";
        foreach(
$r as $ampm)
        {
            
$select .= "<option value=\"$t\"";
            
$select .= ($ampm==$selected) ? ' selected="selected"' '';
            
$select .= ">$ampm</option>\n";
        }
        
$select .= '</select>';
        return 
$select;
    }
?>
<table>
<tr><td>Year</td><td>Month</td><td>Day</td><td>Hour</td><td>Minute</td></tr>
<tr>
<td><?php echo createYears(20002020'start_year'2010); ?></td>

<td><?php echo createMonths('start_month'4); ?></td>

<td><?php echo createDays('start_day'20); ?></td>

<td><?php echo createHours('start_hour'4); ?></td>

<td><?php echo createMinutes('start_minute'30); ?></td>

<td><?php echo createAmPm('start_ampm''am'); ?></td>

</tr>

</table>

Get Text Between HTML Tags

Get Text Between HTML Tags

DO NOT USE REGEX TO PARSE HTML

By using regular expressions with the preg_match() or preg_match_all() functions, the parse is made to work extremely hard as PHP loops over and over the text to find matches. By using the DOM functions the speed is increased dramatically and parsing is much cleaner. This example shows how it might be done with preg_match_all().
<?php
 
/**
 *
 * @get text between tags *
 * @param string (The string with tags) *
 * @param string $tagname (the name of the tag *
 * @return string (Text between tags) *
 */
 
function getTextBetweenTags($string$tagname)
 {
    
$pattern "/<$tagname>(.*?)<\/$tagname>/";
    
preg_match($pattern$string$matches);
    return 
$matches[1];
 }
?>
The above function is very basic, and will not attend to nested tags or check for broken tags. By making use of the PHP DOM extension these issues can be addressed.
The function itself takes three arguements.
$tag
The tag to find the text between
$html
The HTML or XML to be searched
$strict
Tells the function to load in HTML or XML mode, default is HTML mode
The third parameter if set to one allows the function to parse custom tags as found in XML and some XHTML documents.
<?php/**
 *
 * @get text between tags
 *
 * @param string $tag The tag name
 *
 * @param string $html The XML or XHTML string
 *
 * @param int $strict Whether to use strict mode
 *
 * @return array
 *
 */
function getTextBetweenTags($tag$html$strict=0)
{
    
/*** a new dom object ***/
    
$dom = new domDocument;

    
/*** load the html into the object ***/
    
if($strict==1)
    {
        
$dom->loadXML($html);
    }
    else
    {
        
$dom->loadHTML($html);
    }

    
/*** discard white space ***/
    
$dom->preserveWhiteSpace false;

    
/*** the tag by its tag name ***/
    
$content $dom->getElementsByTagname($tag);

    
/*** the array to return ***/
    
$out = array();
    foreach (
$content as $item)
    {
        
/*** add node value to the out array ***/
        
$out[] = $item->nodeValue;
    }
    
/*** return the results ***/
    
return $out;
}
?>
In this example plain HTML is used and no third arguement is supplied to the function. This allows for invalid, or broken HTML. The third paragraph is missing a closing <p> tag, however, with the use of DOM and the loadHTML this deviation is allowed. The example will still parse the HTML and retrieve an array of all the text between all <a> anchor tags.
<?php

$html 
'<body>
<h1>Heading</h1>
<a href="http://phpro.org">PHPRO.ORG</a>
<p>paragraph here</p>
<p>Paragraph with a <a href="http://phpro.org">LINK TO PHPRO.ORG</a></p>
<p>This is a broken paragraph
</body>'
;
$content getTextBetweenTags('a'$html);

foreach( 
$content as $item )
{
    echo 
$item.'<br />';
}
?>
In this final example two custom tags are used such as may be found in XML or XHTML documents. The third parameter is set to one which tells the function to use XML mode and parse the custom tags.
<?php

$xhtml 
'<html>
<body>
<para>This is a paragraph</para>
<para>This is another paragraph</para>
</body>
</html>'
;
$content2 getTextBetweenTags('para'$xhtml1);
foreach( 
$content2 as $item )
{
    echo 
$item.'<br />';
}
?>

Create Thumbnail From Animated GIF


Create Thumbnail From Animated GIF

<?php
try
{
    
/*** Read in the animated gif ***/
    
$animation = new Imagick("animation.gif");

    
/*** Loop through the frames ***/
    
foreach ($animation as $frame)
    {
        
/*** Thumbnail each frame ***/
        
$frame->thumbnailImage(100100);
   
        
/*** Set virtual canvas size to 100x100 ***/
        
$frame->setImagePage(10010000);
    }

    
/*** Write image to disk. Notice writeImages instead of writeImage ***/
    
$animation->writeImages("animation_thumbnail.gif");

    echo 
"Images written";
}
catch(
Exception $e)
{
    echo 
$e->getMessage();
}
?>

Create Thumbnail With GD

Create Thumbnail With GD

In this example, the image is a JPG but other formats can be used in the same manner with imageCreateFromGif and imageGif etc. Note the use of imageCopyResampled to give better image quality than imageCopyResized.

<?php

    error_reporting
(E_ALL);

    
$width 80;

    
/*** the image file to thumbnail ***/
    
$image 'spork.jpg';

    if(!
file_exists($image))
    {
        echo 
'No file found';
    }
    else
    {
        
/*** image info ***/
        
list($width_orig$height_orig$image_type) = getimagesize($image);

        
/*** check for a supported image type ***/
        
if($image_type !== 2)
        {
            echo 
'invalid image';
        }
        else
        {
            
/*** thumb image name ***/
            
$thumb 'thumb.jpg';

            
/*** maintain aspect ratio ***/
            
$height = (int) (($width $width_orig) * $height_orig);

            
/*** resample the image ***/
            
$image_p imagecreatetruecolor($width$height);
            
$image imageCreateFromJpeg($image);
            
imagecopyresampled($image_p$image0000$width$height$width_orig$height_orig);

            
/*** write the file to disc ***/
            
if(!is_writeable(dirname($thumb)))
            {
                echo 
'unable to write image in ' dirname($thumb);
            }
            else
            {
                
imageJpeg($image_p$thumb100);
            }
        }
    }
?>

Underscore To Camel Case

<?php/**
* Convert strings with underscores into CamelCase
*
* @param    string    $string    The string to convert
* @param    bool    $first_char_caps    camelCase or CamelCase
* @return    string    The converted string
*
*/
function underscoreToCamelCase$string$first_char_caps false)
{
    if( 
$first_char_caps == true )
    {
        
$string[0] = strtoupper($string[0]);
    }
    
$func create_function('$c''return strtoupper($c[1]);');
    return 
preg_replace_callback('/_([a-z])/'$func$string);
}
?>
<?php
    $str 
'this_is_under_score_word';
    echo 
underscoreToCamelCase$str);?>