sfNubioAddonFunctionsPlugin - 1.0.3

Adds a helper with multiple functions that perform commonly used tasks.

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


« Back to the Plugins Home

Signin


Forgot your password?
Create an account

Tools

Stats

advanced search
Information Readme Releases Changelog Contribute
Show source

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.

  • If the use_helper function does not exist:

    sfProjectConfiguration::getActive()->loadHelpers(array('AddonFuncs'));

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.

  • Examples:

    is_ip_address( '127.0.0.1' ); //true
    is_ip_address( '127.0.0' ); //false
    is_ip_address( '500.0.0.1' ); //false

is_valid_email

is_valid_email checks if an email address if formatted properly.

  • Examples:

    is_valid_email( 'has-dash@domain.com' ); //true
    is_valid_email( 'two@@signs.com' ); //false
    is_valid_email( 'missingDomain@.com' ); //false

true_percentage

true_percentage returns true a certain percentage of the time.

  • Examples:

    true_percentage( 10 ); //10% of the time, this returns true. 90% of the time, it returns false

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)

  • Examples:

    append_number_suffix( 1 ); //1st
    append_number_suffix( 11 ); //11th
    append_number_suffix( 132626 ); //132626th

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.

  • Examples:

    $var1 = 1;
    $var2 = 2;
    swap_vars( $var1, $var2 ); //$var1 is now 2, and $var2 is now 1

full_http_url

full_http_url gets the complete HTTP URL of the request.

  • Examples:

    full_http_url(); //http://example.com/somefile.php?param=value

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.

  • Examples:

    function some_func() {
        pretty_backtrace(); //prints a nice-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.

  • Examples:

    strpos_arr( 'abcde', array( 'c', 'e' ) ); // 2

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.

  • Examples:

    db_ip2long( '255.255.255.210' ); //4294967250

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.

  • Examples:

    $str = 'abcd'; $ret = string_unshift( $str, 'e', 9 ); //$str ='eeeeeabcd'; $ret = 5; 

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.

  • Examples:

    $str = 'aaaaabcde'; $ret = string_shift( $str, 'a' ); //$str ='bcde'; $ret = 'aaaaa';

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.

  • Examples:

    $str = 'abcd'; $ret = string_push( $str, 'e', 9 ); //$str ='abcdeeeee'; $ret = 5; 

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.

  • Examples:

    $str = 'abcdeeeee'; $ret = string_shift( $str, 'e' ); //$str ='abcd'; $ret = 'eeeee';

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.

  • Examples:

    string_map( 'strtoupper', $string ); //FOOBAR     

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.

  • Examples:

    is_odd( 5 ); //true
    is_odd( 6 ); //false       

is_even

is_even determines whether or not a number is even.

  • Examples:

    is even( 6 ); //true
    is_even( 5 ); //false  

trim_extra_spaces

trim_extra_spaces replaces two or more spaces in a string with a single space.

  • Examples:

    trim_extra_spaces( 'foo      bar' ); //foo bar