CodeIgniter Inflector Helper

The inflector helper file contains some predefined function that allows users to change their English words into plural, singular, and camel case, etc.

Loading an Inflector Helper

Before using an inflector helper function, you must load the helper in the controller's files.

Syntax

$this->load->helper (‘inflector’);

Functions of an Inflector helper:

1`. singular(): It converts plural words into singular words. For example, boys converts into boy, girls converts into girl, etc.

syntax

singular ($str);

$str: It takes an input string to define a singular function.

Example

echo singular ( ‘Boys’ ); // it prints ‘Boy’

2. plural(): It converts singular word into plural. For example, boy converts into boys, girl converts into girls, etc.

syntax

plural ($str);

$str: It takes an input string to define a plural function.

Example

echo plural ( ‘boy’); // It print ‘Boys’

3. camelize(): It converts a compound string to the camel case format. It removes spaces and underscores between words.

Syntax

camelize ($str);

$str: It takes a string, as input.

Example

echo camelize ( ‘my_first_book’); // It prints ‘myFirstBook’
 echo camelize ( ‘my first book’ ); //  It prints ‘myFirstBook’ 

4. underscore(): It converts multiple words into an underscore format. It removes spaces among the words.

Syntax

underscore ($str);

$str: It takes a string, as an input.

Example

echo underscore ("hello CodeIgniter Lovers"); // It prints hello_codeigniter_lovers

5. humanize(): It converts a string into a human-readable format by removing underscores. It adds spaces among the words.

Syntax

humanize ( $str [,
$seperator = ’_’ ] ); 

$str: It defines a string.

$seperator : It defines an input separator such as '-' that extracts all hyphens from the input string and creates the string in a human-readable form.

Example

echo humanize ( ‘welcome_to_inflector_topic’ ); // Welcome To Inflector Topic
 echo humanize ( "welcome-to-inflector-topic", "-"); /* It prints Welcome To Inflector 
 Topic */ 

6. word_is_countable(): It returns a boolean value, if the given input string is countable or not.

Syntax

word_is_countable ($word);

$word: It takes a word as an input.

Example

echo word_is_countable ( ‘boys’) // return 1

Create a simple program of an Inflector helper

Create a controller file in application/controller named Online.php. In this file write the following program:

Online.php

<?php
 class Online extends CI_Controller
 {
     public function hello_function() {
     $this->load->helper('inflector');
     echo "<title> Tutorial and Example </title>";
    // singular() function 
     $str ="Boys";
     echo singular($str)."<br>";
     // plural() function 
     $str1 ="Car";
     echo plural($str1)."<br>";
     // camelize() function
     $str2 ="my_first_book";
     echo camelize($str2)."<br>";
     $str3 ="my first book";
     echo camelize($str3)."<br>"; 
     // underscore() function
     $str6 ="hello CodeIgniter Lovers";
     echo underscore($str6)."<br>";
     $str4 ="welcome to tutorial and example";
     echo underscore($str4)."<br>";
     // humanize() function 
     $str5 ="hello-my_friends";
     echo humanize($str5)."<br>";
     $str5 ="hello_my_friends";
     echo humanize($str5)."<br>";
     echo humanize ( "welcome-to-inflector-topic", "-")."<br>";
     // word_is_countable() function 
     echo word_is_countable("men");
 }
 }
 ?> 

When you execute the above program in localhost by invoking the URL localhost/CodeIgniter-3.1.11/index.php/Online/, it shows the output, as shown below.

CodeIgniter Inflector Helper