sfNubioAddonFunctionsPlugin
The sfNubioAddonFunctionsPlugin is a symfony plugin that provides a
helper with various functions that assist in programming PHP code.
These functions provide commonly used tasks that are often hacked together
in every application, but would benefit from one centralized plugin. This
plugin aims to do that.
Installation
Install the plugin (via a package)
symfony plugin:install sfNubioAddonFunctionsPlugin
Install the plugin (via a Subversion checkout)
svn co http://svn.symfony-project.com/plugins/sfNubioAddonFunctionsPlugin/trunk plugins/sfNubioAddonFunctionsPlugin
Activate the plugin in the config/ProjectConfiguration.class.php
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
$this->enablePlugins(array(
'...',
'sfNubioAddonFunctionsPlugin',
'...'
));
}
}
Usage
As with all helpers, you must use use_helper( 'AddonFuncs' ) in your template files.
Now that the helper is called, you can use one of the functions in it.
is_ip_address
is_ip_address checks if a given string is a valid IPv4 address.
This function only works for IPv4. In a future version, IPv6 may be supported.
is_valid_email
is_valid_email checks if an email address if formatted properly.
true_percentage
true_percentage returns true a certain percentage of the time.
random_string
random_string generates a random string of characters. By default, it generates letters only. However, it can optionally include numbers and/or punctuation. The first parameter is length, the second is if numbers shoudl be included, and the third is if punctuation should be included.
Examples:
random_string( 10 ); //aFsjdcVfES, for example
random_string( 10, true ); //fJFjs49f4s, for example
random_string( 10, true, true ); //fgu3$(vje), for example
append_number_suffix
append_number_suffix appends the proper cardinal suffix (st, nd, rd, or th) to a number (such as 1 -> 1st)
in_string
in_string checks if an string is found in another string. First parameter is the string to search for, second is the string to search in, and third is whether or not to do it case-insensitively (default false).
Examples:
in_string( 'Foo', 'Foobar' ); //true
in_string( 'foo', 'Foobar', true ); //true
in_string( 'foo', 'Foobar' ); //false
swap_vars
swap_vars switches the values of two given variables.
full_http_url
full_http_url gets the complete HTTP URL of the request.
parse_seconds
parse_seconds converts a number of seconds into an array of year, month, week, day, hour, minute, and second values.
Examples:
parse_seconds(62); //array( 'minute' => 1, 'second' => 2)
parse_seconds(12345); //array( 'hour' => 3, 'minute' => 25, 'second' => 45 )
parse_seconds( -123456789 ); //array ('year' => -3, 'week' => -37, 'day' => -4, 'hour' => -21, 'minute' => -33, 'second' => -9 )
pretty_backtrace
pretty_backtrace generates a pretty-looking backtrace.
strpos_arr
strpos_arr checks if an array of values is in a string. The first parameter is the string to search in, and the second parameter is the values to check. The return value is the first position of the first value in the array.
stripos_arr
strpos_arr is the same as `strpos_arr1, except that it searches in a case-insensitive manner.
db_ip2long
db_ip2long converts a 1.2.3.4-formatted IPv4 to an unsigned integer for entry into MySQL databases.
Array-like functions for strings
sfNubioAddonFunctionsPlugin includes various functions similar to array_*() functions built into PHP.
string_unshift
string_unshift adds a character to the beginning of a string as many times as necessary to make it a certain length. The first parameter is a reference to the string, the second is the character to add, and the third is the maximum length of the string. This is similar to PHP's array_unshift() function.
string_shift
string_shift chops a certain character off the beginning of a string. If the character is repeated before another character is found, it will continue chopping until it comes to a different character. The first parameter is a reference to the string, the second is the character to remove. This is similar to PHP's array_shift() function.
string_push
string_push adds a character to the end of a string as many times as necessary to make it a certain length. The first parameter is a reference to the string, the second is the character to add, and the third is the maximum length of the string. This is similar to PHP's array_push() function.
string_pop
string_pop chops a certain character off the end of a string. If the character is repeated before another character is found, it will continue chopping until it comes to a different character. The first parameter is a reference to the string, the second is the character to remove. This is similar to PHP's array_pop() function.
string_map
string_map applies a function to all the characters in a string. The first parameter is the callback function, and the second is the string. This is similar to PHP's array_map() function.
calc_cidr
calc_cidr calculates the starting and ending IPs in a CIDR address. The first parameter is the base IP (the part before the /), and the second is the CIDR (the part after the /)
Examples:
//Converting 127.0.0.1/16
calc_cidr( '127.0.0.1', '16' ); //array( 'begin' => '127.0.0.0', 'end' => '127.0.255.255', 'count' => 65536 )
is_between
is_between determines whether or not a value is between two other values. The first parameter is the value to check, the second is the minimum value, the third is the maximum value, and the fourth is whether or not to use <= and >=, as opposed to just < and > (default true).
Examples:
is_between( 5, 3, 7 ); //true
is_between( 5, 5, 7, false ); //false
is_between( 5, 5, 7 ); //true
Odd/Even checkers
is_odd
is_odd determines whether or not a number is odd.
is_even
is_even determines whether or not a number is even.
trim_extra_spaces
trim_extra_spaces replaces two or more spaces in a string with a single space.