FUEL CMS User Guide : Version 0.9.3


MY String Helper

Contains functions to be used with string. Extends CI's string helpers and is autoloaded.

This helper is loaded using the following code:

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

The following functions are available:

eval_string(str, [vars])

Evaluates a strings PHP code. Used for outputing FUEL page data. The optional vars parameter is an array of variables to be passed to the string.

$str = "This is my <php echo strtoupper('upper case'); ?> string";
eval_string($str); // This is my UPPER CASE string

Because this function evaluates PHP code, make sure the data being sent to it is safe.

lang(str, [vars])

Get translated local strings with arguments.

// in lang file
$lang['lang_key'] = "This is a lang file entry for %1s.";
...
echo lang('lang_key', 'my_param'); //This is lang file entry for my_param.

pluralize(num, str, [plural])

Add an s to the end of string based on the number. If the count is only one, it will not add the s.

echo 'The search returned '.count($results).' '.pluralize(count($results), 'result');

strip_whitespace(str, [vars]).

Strips extra whitespace from a string. It will remove all whitespace with more then one space.

$str = '  my string   without   whitespace';
echo strip_whitespace($str); // my stringwithoutwhitespace

smart_ucwords(str, [exceptions])

Converts words to title case and allows for exceptions. The exceptions parameter is an array of strings that will be ignored.

$str = "the return of the jedi";
echo smart_ucwords($str); // The Return of the Jedi

safe_htmlentities(str, protect_amp)

Safely converts a string's entities without encoding HTML tags and quotes. The protect_amp parameter will protect ampersands as well and is set to TRUE by default.

$str = '

This is a test with special characters : “”‘’™–…&©"

'; echo safe_htmlentities($str, ENT_NOQUOTES, 'UTF-8', FALSE);

php_to_template_syntax(str)

Convert PHP syntax to Dwoo templating syntax. Must use the PHP alternative syntax for if and foreach loops to be translated correctly.

$str = '<?=$my_var?>';
echo php_to_template_syntax($str);//  {$my_var}

parse_template_syntax(str, vars)

Parses a strings Dwoo templating syntax.

$vars = array('name' => 'Luke');
$str = '{name}, I am your Father.';
echo parse_template_syntax($str, $vars);//  Luke, I am your Father.