• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
a couple of potentially useful helpers

#1
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
Reply

#2
This helpers is very useful For me. Thanks
Reply

#3
glad you could use them. Smile
Reply

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

#5
First helper is kinda pointless.
Reply

#6
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.
Reply

#7
(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.
Reply

#8
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).
Reply

#9
(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().
Reply

#10
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
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.