Category Archives: Codeigniter Tutorial

Codeigniter Caching Driver Library


Codeigniter Caching Driver Library – This library provide various functions that are used to store, update and delete a value of variable in cache. $this->load->driver(‘cache’); is used to load the library. Here in this tutorial, we are going to explain how to use caching driver library.


Codeigniter caching driver class library.

Let us first see how to load caching driver library and then use its function-

Load codeigniter caching driver library

How to load codeigniter caching driver library:

 
$this->load->driver('cache');

Functions:-

There are following functions available in caching driver library. Now we will explain one by one with example.

  • 1. Create cache
  • 2. Delete cache
  • 3. Info cache
  • 4. increment cache
  • 5. Decrement cache
  • 6. Clean cache
  • 7. Metadata cache

1. Create cache

Syntax of create cache function is

Syntax of create cache function is:-

get($id)
$this->cache->get('id');

    Parameters :-

  • $id (string) : Cache item name
  • Returns : Item value or FALSE if not found
  • Return type : Mixed

EXAMPLE

Here is simple example of create cache.

Create cache in codeigniter example:-

//Controller
public function createCache()
	{
	   $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));

		if ( ! $text = $this->cache->get('myVariable'))
		{
				echo "Caching Demo
"; $text = 'Welcome to tutorialsplane.com!'; // Save into the cache for 5 minutes $this->cache->save('myVariable', $text, 300); } echo $text; }

When we will run this program. File will be create in cache folder.

The output will be like this –

Caching Driver library

2. Delete cache

Syntax of delete cache function is

Syntax of delete cache function is:-

delete($id)
$this->cache->delete('id');

    Parameters :-

  • $id (string) : Cache item name
  • Returns : TRUE on success, FALSE on failure
  • Return type : Bool

EXAMPLE

Here is simple example of delete cache.

Delete cache in codeigniter example:-

//Controller
public function deleteCache()
	{
      $this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
	  $del = $this->cache->delete('myVariable');
	  if($del==true){
		  echo "Deleted succesfully";
	  }

When we will run this program. File will be delete from cache folder.

The output will be like this –

Caching Driver library

3. Info cache

Syntax of info cache function is

Syntax of info cache function is:-

cache_info()
var_dump($this->cache->cache_info());

    Parameters :-

  • Returns : Information on the entire cache database
  • Return type : Mixed

EXAMPLE

Here is simple example of info cache.

Info cache in codeigniter example:-

//Controller
public function getInfo()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		echo "
";
		var_dump($this->cache->cache_info());
		
	}

This function will return all information about cache file which is placed in cache folder.

The output will be like this –

Codeigniter Caching Driver Library

4. Increment cache

Syntax of increment cache function is

Syntax of increment cache function is:-

increment($id[$offset = 1])

    Parameters :-

  • $id (string) : Cache ID
  • $offset (int) : Step/value to add
  • Returns : New value on success, FALSE on failure
  • Return type : Mixed

EXAMPLE

Here is simple example of increment cache.

Increment cache in codeigniter example:-

//Controller
public function getIncrement()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		echo $this->cache->increment('TutorialsPlane');
		
		echo $this->cache->increment('SolidCoupon', 10);
		
	}

The output will be like this –

Caching Driver library

5. Decrement cache

Syntax of decrement cache function is

Syntax of decrement cache function is:-

decrement($id[$offset = 1])

    Parameters :-

  • $id (string) : Cache ID
  • $offset (int) : Step/value to reduce by
  • Returns : New value on success, FALSE on failure
  • Return type : Mixed

EXAMPLE

Here is simple example of decrement cache.

Decrement cache in codeigniter example:-

//Controller
public function getDecrement()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		echo $this->cache->decrement('TutorialsPlane'); 
		echo $this->cache->decrement('SolidCoupon', 2);
		
	}

The output will be like this –

Caching Driver library

6. Clean cache

Syntax of clean cache function is

Syntax of clean cache function is:-

clean()

    Parameters :-

  • Returns : TRUE on success, FALSE on failure
  • Return type : Bool

EXAMPLE

Here is simple example of clean cache.

Clean cache in codeigniter example:-

//Controller
public function cleancache()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		$this->cache->clean();	
	}

This method will clean all cache folder

7. Metadata cache

Syntax of metadata cache function is

Syntax of metadata cache function is:-

get_metadata($id)

    Parameters :-

  • $id (string) : Cache item name
  • Returns : Metadata for the cached item
  • Return type : mixed

EXAMPLE

Here is simple example of metadata cache.

Metadata cache in codeigniter example:-

//Controller
public function getmetadata()
	{
		$this->load->driver('cache', array('adapter' => 'apc', 'backup' => 'file'));
		var_dump($this->cache->get_metadata('myVariable'));
		
		
	}

This method will return all information about cache file in array format.

The output will be like this –

Codeigniter Benchmarking Class Library


Codeigniter Benchmarking Class library – Codeigniter benchmarking class library provide various functions that are used for enables the time and memory consumption between two marked points. This library is always active and it is also automatically loaded by CodeIgniter. Here in this tutorial, we are going to explain how to use benchmarking class library.


Codeigniter Benchmarking Class library Example

This library is automatically loaded in codeIgniter.

Functions:-

There are two functions available in benchmarking class library. Now we will explain one by one with example.

  • 1. Total time execution
  • 2. Total memory consumption

1. Total time execution

Syntax of total time execution function is

Syntax of total time execution function is:-

$this->benchmark->mark('code_start');
// your coding
$this->benchmark->mark('code_end');
echo $this->benchmark->elapsed_time('code_start', 'code_end');

It has some classes:

  • 1. Mark a start point
  • 2. Mark a end point
  • 3. Run the “time consumption” function to view the results

This function calculates and displays the execution time of program.

EXAMPLE

Here is simple example of total time execution.

Total time execution in codeigniter example:-

//Controllers part
public function bench()
	{
		$this->load->view('library/bench_view');	
	}

// Views parts



The output will be like this –

Codeigniter Benchmarking Class library

2. Total memory consumption

Syntax of total memory consumption function is

Syntax of total memory consumption function is:-

$this->benchmark->mark('code_start');
// your coding
$this->benchmark->mark('code_end');
echo $this->benchmark->memory_usage();

It has also same properties.

This Function Calculates and display the memory consumption of program.

EXAMPLE

Here is simple example of total memory consumption.

Total memory consumption in codeigniter example:-

// Views parts




The output will be like this –

Codeigniter Benchmarking Class library

Codeigniter URL Helper


Codeigniter URL Helper – Codeigniter URL Helper contains functions That is used for parsing the URL And break Down Into Several Segment and pass to the Controller Or store as a Variable.$this->load->helper(‘url’); is used to load the helper. Here in this tutorial, we are going to explain how to use URL helper in CodeIgniter.


Codeigniter URL Helper Example

Let us first see how to load URL helper in codeigniter and then use its function-

Load URL Helper

How To Load URL Helper in Codeingniter Example:

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

Functions:-

There are many functions are available in URL helper. Now we will explain one by one with example.

    • 1. Site URL, Base URL, The current URL
    • 2. An URI string
    • 3. index_page value
    • 4. hyperlink (anchor tag)
    • 5. Pop-up hyperlink
    • 6. A “mail to” hyperlink and safe “mail to” hyperlink
    • 7. Linkified string
    • 8. URL-formatted string
    • 9. Protocol-prefixed URL string
    • 10. redirect

    1. Site URL, Base URL, The current URL

    This Function is used to work with url like Site, Base and Current url:-

    Syntax of Site URL, Base URL, The current URL function is:-

    site_url([$uri = ''[$protocol = NULL]])
    base_url($uri = '', $protocol = NULL)	
    current_url()
    

      Parameters:

    • $uri (string) : URI string
    • Return type : string

    EXAMPLE

    Here is simple example of Site URL, Base URL, The current URL.

    Site URL, Base URL, The current URL in Codeigniter Example:-

    //Controllers part
    public function urlCheck()
    	{
    		$this->load->helper('url');
    		$this->load->view('url_view');
    	}
    
    // Views parts
    

    Site Url



    Base Url



    Current Url

    The output will be like this –

    2. An URI string

    This Function is used to redirect directory path . Syntax of An URI string function is:-

    Syntax of An URI string function is:-

    uri_string()
    

      Parameters:

    • $uri (string) : URI string
    • Returns : uri string
    • Return type : string

    EXAMPLE

    Here is simple example of An URI string.

    An URI string in Codeigniter Example:-

    
    
    // Views parts
    
    

    This Helper return only uri string

    The output will be like this –

    3. index_page value

    Syntax of index_page value function is:-

    Syntax of index_page value function is:-

    index_page()
    

      Parameters:

    • Returns : index_page value
    • Return type : mixed

    EXAMPLE

    Here is simple example of index_page value.

    index_page value in Codeigniter Example:-

    // Views parts
    
    

    The output will be like this –

    4. hyperlink (anchor tag)

    This Function is used to create standard html anchor link based on your local url. It accept three parameters-Uri strings, title and attributes:-

    Syntax of hyperlink (anchor tag) function is:-

    anchor($uri = '', $title = '', $attributes = '')
    

      Parameters:

    • $uri (string) : URI string
    • $title (string) : Anchor title
    • $attributes (mixed) : HTML attributes
    • Returns : HTML hyperlink (anchor tag)
    • Return type : string

    EXAMPLE

    Here is simple example of hyperlink (anchor tag).

    hyperlink (anchor tag) in Codeigniter Example:-

    // Views parts
    
    

    The output will be like this –

    5. Pop-up hyperlink

    Pop-up Hyperlink function is used to create html anchor link on new window. Syntax of Pop-up hyperlink function is:-

    Syntax of Pop-up hyperlink function is:-

    anchor_popup($uri = '', $title = '', $attributes = FALSE)
    

      Parameters:

    • $uri (string) : URI string
    • $title (string) : Anchor title
    • $attributes (mixed) : HTML attributes
    • Returns : Pop-up hyperlink
    • Return type : string

    EXAMPLE

    Here is simple example of Pop-up hyperlink.

    Pop-up hyperlink in Codeigniter Example:-

    // Views parts
     800,'height'=> 600,'scrollbars'=> 'yes','status'=> 'yes',
    'resizable'=> 'yes','screenx'=> 0,'screeny'=> 0,'window_name' => '_blank');
    echo anchor_popup('news/local/123', 'Click Me!', $atts);
    ?>
    

    The output will be like this –

    6. A “mail to” hyperlink and safe “mail to” hyperlink

    This function is used to Create link on mail by these functions. It Has three parameters- email string, Title and attributes. Syntax of A “mail to” hyperlink and safe “mail to” hyperlink function is:-

    Syntax of A “mail to” hyperlink and safe “mail to” hyperlink function is:-

    mailto($email, $title = '', $attributes = '')
    safe_mailto($email, $title = '', $attributes = '')
    

      Parameters:

    • $email (string) : E-mail address
    • $title (string) : Anchor title
    • $attributes (mixed) : HTML attributes
    • Returns : A “mail to” hyperlink and A spam-safe “mail to” hyperlink
    • Return type : string

    EXAMPLE

    Here is simple example of A “mail to” hyperlink and safe “mail to” hyperlink.

    A “mail to” hyperlink and safe “mail to” hyperlink in Codeigniter Example:-

    // Views parts
     800,'height'=> 600,'scrollbars'=> 'yes','status'=> 'yes',
    'resizable'=> 'yes','screenx'=> 0,'screeny'=> 0,'window_name' => '_blank');
    echo anchor_popup('news/local/123', 'Click Me!', $atts);
    ?>
    

    The output will be like this –

    Codeigniter URL Helper

    7. Linkified string

    This function is used to create link of email or url. It accepts three parameters- input string, type and popup. Syntax of Linkified string function is :-

    Syntax of Linkified string function is:-

    auto_link($str, $type = 'both', $popup = FALSE)
    

      Parameters:

    • $str (string) : Input string
    • $type (string) : Link type (‘email’, ‘url’ or ‘both’)
    • $popup (bool) : Whether to create popup links
    • Returns : Linkified string
    • Return type : string

    EXAMPLE

    Here is simple example of Linkified string.

    Linkified string in Codeigniter Example:-

    // Views parts
    
    

    The output will be like this –

    Codeigniter URL Helper

    8. URL-formatted string

    This Function is used to create a human friendly url strings. it accept three parameter-input string, seperetor(this is second parameter which is used to seperated a word) and lowercase (bool). Syntax of URL-formatted string function is:-

    Syntax of URL-formatted string function is:-

    url_title($str, $separator = '-', $lowercase = FALSE)
    

      Parameters:

    • $str (string) : Input string
    • $separator (string) : Word separator
    • $lowercase (bool) : Whether to transform the output string to lower-case
    • Returns : URL-formatted string
    • Return type : string

    EXAMPLE

    Here is simple example of URL-formatted string.

    URL-formatted string in Codeigniter Example:-

    // Views parts
    
    

    The output will be like this –

    Codeigniter URL Helper

    9. Protocol-prefixed URL string

    This Function is used to added http tag in url and pass the url string to the function. Syntax of Protocol-prefixed URL string function is:-

    Syntax of Protocol-prefixed URL string function is:-

    prep_url($str = '')
    

      Parameters:

    • $str (string) : url string
    • Returns : Protocol-prefixed URL string
    • Return type : string

    EXAMPLE

    Here is simple example of Protocol-prefixed URL string.

    Protocol-prefixed URL string in Codeigniter Example:-

    // Views parts
    
    
    

    The output will be like this –

    Codeigniter URL Helper

    10. Redirect

    This Function is used to redirect url with base url. this function accept three parameters-uri strings, methods and code.Syntax of Redirect function is:-

    Syntax of Redirect function is:-

    redirect($uri = '', $method = 'auto', $code = NULL)
    

      Parameters:

    • $uri (string) : URI string
    • $method (string) : Redirect method (‘auto’, ‘location’ or ‘refresh’)
    • $code (string) : HTTP Response code (usually 302 or 303)
    • Return type : void

    EXAMPLE

    Here is simple example of Redirect.

    Redirect in Codeigniter Example:-

    // Views parts
    
    
    

    The output will be like this –

Codeigniter XML Helper


Codeigniter XML Helper – Codeigniter XML Helper contains function that is used to Convert String As a input to XML Characters.$this->load->helper(‘xml’); is used to load the helper. Here in this tutorial, we are going to explain how to use XML helper in CodeIgniter.


Codeigniter XML Helper Example

Let us first see how to load XML helper in codeigniter and then use its function-

Load XML Helper

How To Load XML Helper in Codeingniter Example:

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

Functions:-

Following function is available in codeigniter let us understand with example.

    • XML-converted string

    Syntax of XML-converted string function is

    Syntax of XML-converted string function is:-

    xml_convert($str[$protect_all = FALSE])
    

      Parameters:

    • $str (string) : the text string to convert
    • $protect_all (bool) : Whether to protect all content that looks like a potential entity instead of just numbered entities, e.g. &foo;
    • Returns : XML-converted string
    • Return type : string

    EXAMPLE

    Here is simple example of XML-converted string.

    XML-converted string in Codeigniter Example:-

    //Controllers part
    public function xmlCheck()
    	{
    		$this->load->helper('xml');
    		$this->load->view('xml_view');
    	}
    
    // Views parts
    Hi This is Solid Coupon ({).

    '; $string = xml_convert($string); echo $string; ?>

    This Helper Convert String As a Input To XML Characters

    The output will be like this –

    Codeigniter XML Helper

Codeigniter Typography Helper


Codeigniter Typography Helper – Codeigniter Typography Helper contains functions it will help us Formatting the text in an appropriate manner likes wrapping with paragraphs, convert break line into dash and ellipses. $this->load->helper(‘typography’); is used to load the helper. Here in this tutorial, we are going to explain how to use Typography helper in CodeIgniter.


Codeigniter Typography Helper Example

Let us first see how to load Typography helper in codeigniter and then use its function-

Load Typography Helper

How To Load Typography Helper in Codeingniter Example:

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

Functions:-

There are many functions are available in Typography helper. Now we will explain one by one with example.

    • 1. typography-safe string
    • 2. formatted line breaks string
    • 3. decoded entities string

    1. typography-safe string

    Syntax of typography-safe string function is

    Syntax of typography-safe string function is:-

    auto_typography($str[$reduce_linebreaks = FALSE])	
    

      Parameters:

    • $str (string) : Input string
    • $reduce_linebreaks (bool) : Whether to reduce multiple instances of double newlines to two
    • Returns : HTML-formatted typography-safe string
    • Return type : string

    EXAMPLE

    Here is simple example of typography-safe string.

    typography-safe string in Codeigniter Example:-

    //Controllers part
    public function typographyCheck()
    	{
    		$this->load->helper('typography');
    		$this->load->view('typography_view');
    	}
    
    // Views parts
    
    

    This Helper Convert New Lines to Paragraph

    The output will be like this –

    Codeigniter Typography Helper

    2. formatted line breaks string

    Syntax of formatted line breaks string function is

    Syntax of formatted line breaks string function is:-

    nl2br_except_pre($str)	
    

      Parameters:

    • $str (string) : Input string
    • Returns : String with HTML-formatted line breaks
    • Return type : string

    EXAMPLE

    Here is simple example of formatted line breaks string.

    formatted line breaks string in Codeigniter Example:-

    
    
    // Views parts
    
    

    This Helper Convert New Lines to Break Lines

    The output will be like this –

    Codeigniter Typography Helper

    3. decoded entities string

    Syntax of decoded entities string function is

    Syntax of decoded entities string function is:-

    entity_decode($str, $charset = NULL)
    

      Parameters:

    • $str (string) : Input string
    • $charset (string) : Character set
    • Returns : String with decoded HTML entities
    • Return type : string

    EXAMPLE

    Here is simple example of decoded entities string.

    decoded entities string in Codeigniter Example:-

    
    
    // Views parts
    
    

    The output will be like this –

Codeigniter Text Helper


Codeigniter Text Helper – Codeigniter text helper contains functions it will help us to do work with string. it has word limiter, character limiter and lots of function.$this->load->helper(‘text’); is used to load the helper. Here in this tutorial, we are going to explain how to use text helper in codeIgniter.


Codeigniter Text Helper Example

Let us first see how to load text helper in codeigniter and then use its function-

Load text helper

How to load text helper in codeingniter example:

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

Functions:-

There are many functions available in text helper. Now we will explain one by one with example.

    • 1. Word-limited string
    • 2. Character-limited string
    • 3. ASCII values converted to entities
    • 4. accented characters converted
    • 5. Censored string
    • 6. highlighted code string
    • 7. highlighted phase string
    • 8. Word-wrapped string
    • 9. Ellipsized string

    1. Word-limited string

    Syntax of Word-limited string function is

    Syntax of Word-limited string function is:-

    word_limiter($str[$limit = 100[$end_char = '…']])	
    

      Parameters:

    • $str (string) : Input string
    • $limit (int) : Limit
    • $end_char (string) : End character (usually an ellipsis)
    • Returns : Word-limited string
    • Return type : string

    This Function is remove extra word to the number of words specified.

    EXAMPLE

    Here is simple example of word-limited string.

    Word-limited string in codeigniter example:-

    //Controllers part
    public function textCheck()
    	{
    		$this->load->helper('text');
    		$this->load->view('text_view');
    	}
    
    // Views parts
    
    

    The output will be like this –

    Codeigniter Text Helper

    2. Character-limited string

    Syntax of character-limited string function is

    Syntax of character-limited string function is:-

    character_limiter($str[$n = 500[$end_char = '…']])	
    

      Parameters:

    • $str (string) : Input string
    • $n (int) : Number of characters
    • $end_char (string) : End character (usually an ellipsis)
    • Returns : Character-limited string
    • Return type : string

    This Function is remove extra characters to the number of characters specified.

    EXAMPLE

    Here is simple example of character-limited string.

    Character-limited string in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    Codeigniter Text Helper

    3. ASCII values converted to entities

    Syntax of ASCII values converted to entities function is

    Syntax of ASCII values converted to entities function is:-

    ascii_to_entities($str)	
    

      Parameters:

    • $str (string) : Input string
    • Returns : A string with ASCII values converted to entities
    • Return type : string

    EXAMPLE

    Here is simple example of ASCII values converted to entities.

    ASCII values converted to entities in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    4. accented characters converted

    Syntax of accented characters converted function is

    Syntax of accented characters converted function is:-

    convert_accented_characters($str)
    

      Parameters:

    • $str (string) : Input string
    • Returns : A string with accented characters converted
    • Return type : string

    EXAMPLE

    Here is simple example of accented characters converted.

    Accented characters converted in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    5. Censored string

    Syntax of censored string function is

    Syntax of censored string function is:-

    word_censor($str, $censored[$replacement = ''])
    

      Parameters:

    • $str (string) : Input string
    • $censored (array) : List of bad words to censor
    • $replacement (string) : What to replace bad words with
    • Returns : Censored string
    • Return type : string

    EXAMPLE

    Here is simple example of Censored string.

    Censored string in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    6. highlighted code string

    Syntax of highlighted code string function is

    Syntax of highlighted code string function is:-

    highlight_code($str)
    

      Parameters:

    • $str (string) : Input string
    • Returns : String with code highlighted via HTML
    • Return type : string

    EXAMPLE

    Here is simple example of highlighted code string.

    Highlighted code string in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    Codeigniter Text Helper

    7. Highlighted phase string

    Syntax of highlighted phase string function is

    Syntax of highlighted phase string function is:-

    highlight_phrase($str, $phrase[$tag_open = ''[$tag_close = '']])
    

      Parameters:

    • $str (string) : Input string
    • $phrase (string) : Phrase to highlight
    • $tag_open (string) : Opening tag used for the highlight
    • $tag_close (string) : Closing tag for the highlight
    • Returns : String with a phrase highlighted via HTML
    • Return type : string

    EXAMPLE

    Here is simple example of highlighted phase string.

    Highlighted phase string in codeigniter example:-

    
    // Views parts
    ', '');
    ?>
    

    The output will be like this –

    Codeigniter Text Helper

    8. Word-wrapped string

    Syntax of word-wrapped string function is

    Syntax of word-wrapped string function is:-

    word_wrap($str[$charlim = 76])
    

      Parameters:

    • $str (string) : Input string
    • $charlim (int) : Character limit
    • Returns : Word-wrapped string
    • Return type : string

    EXAMPLE

    Here is simple example of Word-wrapped string.

    Word-wrapped string in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    Codeigniter Text Helper

    9. Ellipsized string

    Syntax of ellipsized string function is

    Syntax of ellipsized string function is:-

    ellipsize($str, $max_length[$position = 1[$ellipsis = '…']])
    

      Parameters:

    • $str (string) : Input string
    • $max_length (int) : String length limit
    • $position (mixed) : Position to split at (int or float)
    • $ellipsis (string) : What to use as the ellipsis character
    • Returns : Ellipsized string
    • Return type : string

    EXAMPLE

    Here is simple example of Ellipsized string.

    Ellipsized string in codeigniter example:-

    
    // Views parts
    
    

    The output will be like this –

    Codeigniter Text Helper

Codeigniter String Helper


Codeigniter String Helper – Codeigniter string helper contains functions that will make a random string based on the type and length of arguments. $this->load->helper(‘string’); is used to load the helper. The string helper file contains functions that assist in working with strings . Here in this tutorial, we are going to explain how to use string helper in codeIgniter.


Codeigniter string helper example

Let us first see how to load string helper in codeigniter and then use its function-

Load string helper

How to load string helper in codeingniter example:

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

Functions:-

There are many functions available in string helper. Now we will explain one by one with example.

    • 1. random string
    • 2. incremented string
    • 3. Alternated string
    • 4. Repeated string
    • 5. normalized slashes string
    • 6. stripped slashes string
    • 7. Slash-trimmed string
    • 8. Reduced string
    • 9. quotes converted string
    • 10. quotes stripped string

    1. Random string

    Syntax of random string function is

    Syntax of random string function is:-

    random_string([$type = 'alnum'[$len = 8]])	
    

      Parameters:

    • $type (string) : Randomization type
    • $len (int) : Output string length
    • Returns : A random string
    • Return type : string

    This function generates a random string based on the type and length you specify.

    EXAMPLE

    Here is simple example of random string.

    Random string in codeigniter example:-

    //Controllers part
    public function stringCheck()
    	{
    		$this->load->helper('String');
    		$this->load->view('string_view');
    	}
    
    // Views parts
    
    





    The output of the above example will be something like this –

    2. Incremented string

    Syntax of incremented string function is

    Syntax of incremented string function is:-

    increment_string($str[$separator = '_'[$first = 1]])	
    

      Parameters:

    • $str (string) : Input string
    • $separator (string) : Separator to append a duplicate number with
    • $first (int) : Starting number
    • Return : An incremented string
    • Return type : string

    EXAMPLE

    Here is simple example of incremented string.

    Incremented string in codeigniter example:-

    
    // Views parts
    
    

    If you run the above example then it will produce output something like this –

    3. Alternated string

    Syntax of alternated string function is

    Syntax of alternated string function is:-

    alternator($args)	
    

      Parameters:

    • $args (mixed) : A variable number of arguments
    • Return : Alternated string(s)
    • Return type : mixed

    EXAMPLE

    Here is simple example of alternated string.

    Alternated string in codeigniter example:-

    
    // Views parts
    
    

    The output of the above example will be something like this –

    4. Repeated string

    Syntax of repeated string function is

    Syntax of repeated string function is:-

    repeater($data[$num = 1])	
    

      Parameters:

    • $data (string) : Input
    • $num (int) : Number of times to repeat
    • Return : Repeated string
    • Return type : string

    EXAMPLE

    Here is simple example of repeated string.

    Repeated string in codeigniter example:-

    
    // Views parts
    
    
    

    The output of the above example will be something like this –

    5. Normalized slashes string

    Syntax of normalized slashes string function is

    Syntax of normalized slashes string function is:-

    reduce_double_slashes($str)	
    

      Parameters:

    • $str (string) : Input string
    • $num (int) : Number of times to repeat
    • Return : A string with normalized slashes
    • Return type : string

    This helper converts double slashes string to single slashes.

    EXAMPLE

    Here is simple example of normalized slashes string.

    Normalized slashes string in codeigniter example:-

    
    // Views parts
    
    
    

    The output of example will be like this –

    6. Stripped slashes string

    Syntax of stripped slashes string function is

    Syntax of stripped slashes string function is:-

    strip_slashes($data)	
    

      Parameters:

    • $data (mixed) : Input string or an array of strings
    • Return : String(s) with stripped slashes
    • Return type : mixed

    This helper remove all slashes from string.

    EXAMPLE

    Here is simple example of stripped slashes string.

    Stripped slashes string in codeigniter example:-

    
    // Views parts
    
    
    

    The output of example will be like this –

    7. Slash-trimmed string

    Syntax of slash-trimmed string function is

    Syntax of slash-trimmed string function is:-

    trim_slashes($str)	
    

      Parameters:

    • $str (string) : Input string
    • Return : Slash-trimmed string
    • Return type :string

    This helper delete all leading or trailing slashes from string.

    EXAMPLE

    Here is simple example of slash-trimmed string.

    Slash-trimmed string in codeigniter example:-

    
    // Views parts
    
    
    
    

    The output of example will be like this –

    8. Reduced string

    Syntax of reduced string function is

    Syntax of reduced string function is:-

    reduce_multiples($str[$character = ''[$trim = FALSE]])	
    

      Parameters:

    • $str (string) : Text to search in
    • $character (string) : Character to reduce
    • $trim (bool) : Whether to also trim the specified character
    • Return : Reduced string
    • Return type :string

    This helper delete multiple instance of a particular characters.

    EXAMPLE

    Here is simple example of Reduced string.

    Reduced string in codeigniter example:-

    
    // Views parts
    
    

    The output of example will be like this –

    9. Quotes converted string

    Syntax of quotes converted string function is

    Syntax of quotes converted string function is:-

    quotes_to_entities($str)	
    

      Parameters:

    • $str (string) : Input string
    • Return : String with quotes converted to HTML entities
    • Return type :string

    This helper converts single and double quotes in a string.

    EXAMPLE

    Here is simple example of quotes converted string.

    Quotes converted string in codeigniter example:-

    
    // Views parts
    
    
    

    The output of example will be like this –

    10. Quotes stripped string

    Syntax of quotes stripped string function is

    Syntax of quotes stripped string function is:-

    strip_quotes($str)	
    

      Parameters:

    • $str (string) : Input string
    • Return : String with quotes stripped
    • Return type :string

    This helper delete single and double quotes to string.

    EXAMPLE

    Here is simple example of quotes stripped string.

    Quotes stripped string in codeigniter example:-

    
    // Views parts
    
    
    

    The output of example will be like this –

Codeigniter Smiley Helper


Codeigniter Smiley Helper – Codeigniter smiley helper contains functions that help us to manage emoticons. $this->load->helper(‘smiley’); is used to load the helper. It has a renderer that takes plain text smileys, like 🙂 and turns them into a image representation, like smile!. Here in this tutorial we are going to explain how to use smiley helper in codeigniter.


Codeigniter smiley helper example

Let us first see how to load smiley helper in codeigniter and then use its function-

Load smiley helper

How to load smiley helper in codeingniter example:

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

Functions:-

There are many functions are available in smiley helper. Now we will explain one by one with example.

    • 1. Clickable_smileys
    • 2. Smiley_js
    • 3. Parse_smileys

    1. Clickable_smileys

    Syntax of clickable_smileys function is

    Syntax of clickable_smileys function is:-

    get_clickable_smileys($image_url[$alias = ''[$smileys = NULL]])	
    

      Parameters:

    • $image_url (string) : URL path to the smileys directory
    • $alias (string) : Field alias
    • Returns : An array of ready to use smileys
    • Return type : array

    This function contain your smiley images wrapped in a clickable link.

    EXAMPLE

    Here is simple example of clickable_smileys.

    Clickable_smileys in codeigniter example:-

    //Controllers part
    public function securityCheck()
    	{
    		$this->load->helper('smiley');
    		$this->load->view('smiley_view');
    	}
    
    // Views parts
    
    
    

    Where $smileyFolderPath is the folder path that contains the smiley images.

    The output of the above example will be something like this –

    2. Smiley_js

    Syntax of smiley_js function is

    Syntax of smiley_js function is:-

    smiley_js([$alias = ''[$field_id = ''[$inline = TRUE]]])		
    

      Parameters:

    • $alias (string) : Field alias
    • $field_id (string) : Field ID
    • $inline (bool) : Whether we’re inserting an inline smiley
    • Returns : Smiley-enabling JavaScript code
    • Return type : String

    This function is designed to be placed into the area of your web page.

    EXAMPLE

    Here is simple example of smiley_js.

    Smiley_js in codeigniter example:-

    // Views parts
    
    
    

    The output of the above example will be something like this –

    3. Parse_smileys

    Syntax of parse_smileys function is

    Syntax of parse_smileys function is:-

    parse_smileys([$str = ''[$image_url = ''[$smileys = NULL]]])		
    

      Parameters:

    • $str (string) : Text containing smiley codes
    • $image_url (string) : URL path to the smileys directory
    • $smileys (array) : An array of smileys
    • Returns : Parsed smileys
    • Return type : String

    In this function first parameter must contain your string, the second must contain the URL to your smiley folder.

    EXAMPLE

    Here is simple example of parse_smileys.

    Parse_smileys in codeigniter example:-

    // Views parts
    
    
    

    The output of the above example will be something like this –

Codeigniter Security Helper


Codeigniter Security Helper – Codeigniter security helper is used to file contains security-related functions such as xss_clean (), which will filter out any codes that may be used in cross-site scripting hack. $this->load->helper(‘html’); is used to load the helper. Here in this tutorial we are going to explain how to use security helper in codeigniter.


Codeigniter security helper example

Let us first see how to load security helper in codeigniter and then use its function-

Load security helper

How to load security helper in codeingniter example:

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

Functions:-

There are many functions are available in security helper. Now we will explain one by one with example.

  • 1. XSS-clean string
  • 2. Sanitized file name
  • 3. Hex-formatted hash
  • 4. strip image tags
  • 5. encode php tags

1. XSS-clean string

Syntax of XSS-clean string function is

Syntax of XSS-clean string function is:-

xss_clean($str[$is_image = FALSE])		

    Parameters:

  • $str (string) : Input data
  • $is_image (bool) : Whether we’re dealing with an image
  • Returns : XSS-clean string
  • Return type : String

This function provides cross site script hack filtering.

EXAMPLE

Here is simple example of XSS-clean string.

XSS-clean string in codeigniter example:-

//Controllers part
public function securityCheck()
	{
		$this->load->helper('security');
		$this->load->view('security_view');
	}

// Views parts


The output of the above example will be something like this –

2. Sanitized file name

Syntax of sanitized file name function is

Syntax of sanitized file name function is:-

sanitize_filename($filename)		

    Parameters:

  • $filename (string) : Filename
  • Returns : Sanitized file name
  • Return type : String

This function provides protection against directory traversal

EXAMPLE

Here is simple example of sanitized file name.

Sanitized file name in codeigniter example:-

// Views parts


The output of the above example will be something like this –

3. Hex-formatted hash

Syntax of hex-formatted hash function is

Syntax of hex-formatted hash function is:-

do_hash($str[$type = 'sha1'])		

    Parameters:

  • $str (string) : Input
  • $type (string) : Algorithm
  • Returns : Hex-formatted hash
  • Return type : String

This function give permits you to create one way hashes suitable for encrypting passwords

EXAMPLE

Here is simple example of hex-formatted hash.

Hex-formatted hash in codeigniter example:-

// Views parts

 


The output of the above example will be something like this –

4. Strip image tags

Syntax of strip image tags function is

Syntax of strip image tags function is:-

strip_image_tags($str)		

    Parameters:

  • $str (string) : Input string
  • Returns : The input string with no image tags
  • Return type : String

Security function that will strip image tags from a string. It leaves the image URL as plain text

EXAMPLE

Here is simple example of strip image tags.

Strip image tags in codeigniter example:-

// Views parts

The output of the above example will be something like this –

5. Encode php tags

Syntax of encode php tags function is

Syntax of encode php tags function is:-

encode_php_tags($str)		

    Parameters:

  • $str (string) : Input string
  • Returns : Safely formatted string
  • Return type : String

This security function that converts PHP tags to entities.

EXAMPLE

Here is simple example of encode php tags.

Encode php tags in codeigniter example:-

// Views parts

The output of the above example will be something like this –