CodeIgniter Forums
a couple of potentially useful helpers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: a couple of potentially useful helpers (/showthread.php?tid=32)



a couple of potentially useful helpers - Hobbes - 10-24-2014

These two helpers are a couple i use in my projects. Not sure if anyone will find them useful or not, but thought i would share them just in case.

the first one is an alias function for both the php functions is_null() and empty()

path/file name: application/helpers/variable_helper.php

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


if (! 
function_exists('is_null_or_empty'))
{
    
/**
     * this function combines the is_null() and empty() php functions into one check instead of two
     * 
     * @access public
     * @param var mixed
     * @return boolean
     */
    
function is_null_or_empty($var)
    {
        if (
is_null($var) OR empty($var))
        {
            return 
TRUE;
        }
        else
        {
            return 
FALSE;    
        }
    }



the next one contains two files. a config file and a helper file. It converts html to bbcode and bbcode to html.

config file:

in application/config/autoload.php add the below config file to your autoload config array.
path/file name: application/config/bbcode.php

PHP Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
| -------------------------------------------------------------------
| BBCODE
| -------------------------------------------------------------------
| $config['bbcode'] - an array containing the allowable bbcode tags.

| $config['htmlcode'] - an array containing the bbcode's html equivalent
|
*/
$config['bbcode'] = array(
          
'<',
          
'>',
          
'[list]',
        
'[*]',
        
'[/list]',
        
'[img]',
        
'[/img',
        
'[b]',
        
'[/b]',
        
'[u]',
        
'[/u]',
        
'[i]',
        
'[/i]',
        
'[color="',
        
'[/color]',
        
'[size="',
        
'[/size]',
        
'[url="',
        
'[/url]',
        
'[mail="',
        
'[/mail]',
        
'[code]',
        
'[/code]',
        
'[quote]',
        
'[/quote]',
         
'"]'
      
);

$config['htmlcode'] = array(
        
'&lt;',
        
'&gt;',
        
'<ul>',
        
'<li>'
        
'</ul>',
        
'<img src="',
        
'">',
        
'<b>',
        
'</b>',
        
'<u>',
        
'</u>',
        
'<i>',
        
'</i>',
        
'<span style="color:',
        
'</span>',
        
'<span style="font-size:',
        
'</span>',
        
'<a href="',
        
'</a>',
        
'<a href="mailto:',
        
'</a>',
        
'<code>',
        
'</code>',
        
'<blockquote>',
        
'</blockquote>',
        
'">'
      
); 

add the below file to your autoload helpers array.
path/file name: application/helpers/bbcode_helper.php

PHP Code:
<?php   if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if (! 
function_exists('bb2html'))
{
    function 
bb2html($text)
    {
        
$CI =& get_instance();
        
$bbcode $CI->config->item('bbcode');
        
$htmlcode $CI->config->item('htmlcode');
      
        
$j count($bbcode);
          
$newtext $text;
          for (
$i 0$i $j$i++)
          {
              
$newtext str_replace($bbcode[$i], $htmlcode[$i], $newtext);
          }
          
$newtext xss_clean($newtext);
      
          
$newtext nl2br($newtext);//second pass
          
return $newtext;
    }
}

if (! 
function_exists('html2bb'))
{
    function 
html2bb($text)
    {
        
$CI =& get_instance();
        
$bbcode $CI->config->item('bbcode');
        
$htmlcode $CI->config->item('htmlcode');
      
          
$j count($bbcode);
         
$newtext $text;
        for (
$i 0$i $j$i++)
        {
            
$newtext str_replace($htmlcode[$i], $bbcode[$i], $newtext);
        }
          
$newtext xss_clean($newtext);
      
          return 
$newtext;
    }


well there you go, hopefully someone finds them useful


RE: a couple of potentially useful helpers - Ivan's - 10-26-2014

This helpers is very useful For me. Thanks


RE: a couple of potentially useful helpers - Hobbes - 10-27-2014

glad you could use them. Smile


RE: a couple of potentially useful helpers - Narf - 10-27-2014

You don't need is_null_or_empty(), empty() will return TRUE for NULLs.


RE: a couple of potentially useful helpers - marcogmonteiro - 10-31-2014

First helper is kinda pointless.


RE: a couple of potentially useful helpers - Hobbes - 10-31-2014

i created that helper quite awhile ago when i was getting errors passing nulls into the empty function. It was quite irritating, so basically it was put in as a work around for the problem. Still to this day not sure why i was getting that error with the empty function but yeah, that is why i wrote it.


RE: a couple of potentially useful helpers - Narf - 10-31-2014

(10-31-2014, 07:59 AM)Hobbes Wrote: i created that helper quite awhile ago when i was getting errors passing nulls into the empty function. It was quite irritating, so basically it was put in as a work around for the problem. Still to this day not sure why i was getting that error with the empty function but yeah, that is why i wrote it.

You might've had problems with passing function return values directly to empty():

PHP Code:
// This only works on PHP 5.5+
empty(foo()); 

This has nothing to do with nulls though.


RE: a couple of potentially useful helpers - Avenirer - 10-31-2014

Maybe the problem then (and now) was that what is considered an empty variable is not necessarily a null one. Smile Maybe a better function would be is_empty_and_null($var).


RE: a couple of potentially useful helpers - Narf - 11-01-2014

(10-31-2014, 12:47 PM)Avenirer Wrote: Maybe the problem then (and now) was that what is considered an empty variable is not necessarily a null one. Smile Maybe a better function would be is_empty_and_null($var).

Which is just is_null().


RE: a couple of potentially useful helpers - Hobbes - 11-01-2014

no i do not typically pass a function call to the empty function. just variables.

Plus, for me mainly being a C# and VB.Net developer using is_null_or_empty() is more natural. Even though at this point it is a useless function. lol