Skip to content
Jeff Johns edited this page Jan 31, 2014 · 2 revisions
Name Path
Data Helper /application/helpers/data_helper.php

These methods help manipulate data (typically strings) for use in the application.

Load the helper

$this->load->helper('data_helper');

Methods

decodeValue

Will decode a previously encoded string.

Arguments

Variable Type Default Required Description
$str string N/A Yes The string to decode

Example

$string = decodeValue($string);

findPage

Inspects the URL and looks at the last segment to extract a possible page number from.

Example

// URL /marks/today/5
$page = findPage();

// $page = 5

// URL /marks/2014
$page = findPage();

// $page = 1

findStartFinish

Attempts to find the timestamp to use for start and finish date lookups.

Arguments

Variable Type Default Required Description
$start string N/A Yes The date or timeframe to find the start date from.
$finish string null No The finish date or timeframe. If not send the function will figure it out for you.

Example

// Returns the correct timestamps
$dates = findStartFinish('4 days ago', 'today');

// Returns start as first day of the year and finish as last day of the year
$dates = findStartFinish(2013);

formatErrors

Returns an array of formatted errors.

Arguments

Variable Type Default Required Description
$errors Mixed N/A Yes Either an array of errors or an error number.

Example

// Return array with key of 100 and message of Security token is invalid.
$errors = formatErrors(100);

// Returns array as it was sent
$errors = formatErrors(array(102 => array('email' => 'Email address is invalid')));

generateSlug

Takes a string and returns a valid slug to be used for lookups and within a url.

Arguments

Variable Type Default Required Description
$str String N/A Yes The string to used to create a slug.

Example

// Return my-slug
$errors = formatErrors('My Slug!');

generateTimeSpan

Used to return a nice time when an item was created. Instead of showing the exact datetime of an item it will return something like '10 days ago'.

Arguments

Variable Type Default Required Description
$date Mixed N/A Yes A string of a date, data or datetime.

Example

// Returns 1 day ago (assuming today is 2014-01-30)
$nice_time = generateTimeSpan('2014-01-29');

getLastJsonError

Will simply return the last JSON error if any after parsing a JSON document. If no error it will return false.

Example

$json = json_decode($file);
$json_error = getLastJsonError();

if ($json_error === false) {
    // do something
}
else {
    // Log exception
}

getSmartLabelInfo

Pass a URL to this function and it will return an array giving you the domain, path and key (md5 hash of domain).

Arguments

Variable Type Default Required Description
$url String N/A Yes The URL to extract information from.

Example

// Returns array of domain: nilai.co, path: , key: ea494e43dd071dc289fbe07c5d05f574
$smart_info = getSmartLabelInfo('http://nilail.co');

// Returns array of domain: nilai.co, path: /marks, key: ea494e43dd071dc289fbe07c5d05f574
$smart_info = getSmartLabelInfo('nilail.co/marks');

// Returns array of domain: nilai.co, path: /search, key: ea494e43dd071dc289fbe07c5d05f574
$smart_info = getSmartLabelInfo('http://www.nilail.co/search');

getTagsFromHash

Returns the words attached to hashmarks in a string.

Arguments

Variable Type Default Required Description
$str String N/A Yes The string to extract tags from.

Example

// Returns array of 0: hash, 1: tag, 2: jeff
$tags = getSmartLabelInfo('This is my string. #hash #tag #jeff');

purifyHTML

Removes any unwanted tags from the string, replaces new lines with breaks and decodes the string if already encoded.

Arguments

Variable Type Default Required Description
$str string N/A Yes The string to purify

Example

$string = purifyHTML($string);

readEasy

Takes a string, replaces and underscores or dashes with spaces and captilized each word.

Arguments

Variable Type Default Required Description
$str string N/A Yes The string to ready

Example

// Returns About Us
$string = readEasy('about_us');

readyDomain

Purifies a domain name before getting put into the DB. Basically strips the protocol off.

Arguments

Variable Type Default Required Description
$domain string N/A Yes The domain to ready

Example

// Returns www.google.com
$string = readyDomain('http://www.google.com');

standardizePath

Takes a string and makes sure it starts with a slash and does NOT end with a slash.

Arguments

Variable Type Default Required Description
$url string N/A Yes The url to update

Example

// Returns /test/about-us
$string = readyDomain('test/about-us/');

stripTags

Sort of like PHP's strip_tags method except it works in reverse. You specify the tags to remove not keep.

Arguments

Variable Type Default Required Description
$str string N/A Yes The string to strip tags from.
$tags array N/A Yes An array of tags to strip from the submitted string.

Example

$string = stripTags($string, array('font','iframe','script'));

Clone this wiki locally