×

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

Related Topics

CodeIgniter Output class

Output class The CodeIgniter framework contains a core class that is an Output class. It  includes the various functions that are used to send the final web page to the requesting browser. In...

8 minutes read.

Retrieve data from Database

Retrieve data from the Database Now we will discuss how we can retrieve data from the database that we have inserted to the employees table of the database in our previous topic “inserted...

2 minutes read.

CodeIgniter Email Class

CodeIgniter provides an e-mail library that helps to send a message from one application to another. You can create and send text messages easily in CodeIgniter application even you can set email...

9 minutes read.

CodeIgniter Security Helper

A Security helper file contains some predefined functions that are used to protect application from unauthorized access. Loading the Helper The following syntax is used to load the security helper in the CodeIgniter application. Syntax $this-> load->...

4 minutes read.

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...

3 minutes read.

CodeIgniter Controller

As its name suggest that it is a Controller that controls the web application, and it works as an intermediary between the Models and the View. A controller takes requests from the user...

3 minutes read.

CodeIgniter Calendaring Library

The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar design. Load...

7 minutes read.

CodeIgniter Input Class

Input Class The CodeIgniter framework provides an input class that includes various input functions to pre-process global input data for protection; furthermore, it also provides some helper functions to obtain input data. This...

11 minutes read.

CodeIgniter Encrypt Class

The Codeigniter provides an encrypt class used to protect confidential data stored on computer systems or data transmitted over a network. It provides two-way data encryption techniques such as Mcrypt PHP extension and...

4 minutes read.

CodeIgniter Shopping Cart Class

It contains the various function that enables the user to add or update, display, and delete data item from the shopping carts while browsing the site. The items added in the cart...

10 minutes read.

CodeIgniter File Helper

The file helper function is used to perform various tasks with files such as accessing files, writing data to files, deleting files, and more. Loading the File Helper Before using the file helper, you must...

7 minutes read.

CodeIgniter Pagination Class

Pagination Class The CodeIgniter framework provides a pagination class that is used in the data list to create pagination links in a web application. Generally, it is used to retrieve data records from...

10 minutes read.

Calendaring Class CodeIgniter

Calendaring Class The calendar library contains various functions that are used to create a dynamic calendar. It also enables you to pass the data to calendar cells and 100% control over the calendar...

6 minutes read.

Database Configuration CodeIgniter

In every application, there is an important part of a database through which developers can connect an application to store users’ data and perform various functions in the database application such as inserting,...

10 minutes read.

Codeigniter Session Library

Session Library The session is an essential part of any application. It contains various functions to manage the users' status and track their activity while browsing on the site. For example,...

13 minutes read.

CodeIgniter Libraries

The library is an important part of the CodeIgniter framework. It has a large collection of libraries files that are used to increase the performance of an application. By default, all CodeIgniter libraries...

4 minutes read.

CodeIgniter Text Helper

The text helper contains the various function that is used to operate with text such as define a word limit and characters limit, conversion of word from ASCII to entities, etc. Loading the text...

4 minutes read.

CodeIgniter Language Class

Language Class The CodeIgniter’s language class contains the various functionality that is used to access the language-specific files and text line for the purposes of internationalization. This class provides you suitability to add or...

4 minutes read.

Directory Helper Codeigniter

Directory Helper CodeIgniter: provides a directory helper function that works with the directory. It shows the structure of the file, hidden file in the folder, and the folder in...

3 minutes read.

CodeIgniter Array Helper

The Array Helper contains some predefined functions that are used to perform the various operation with array. Load Array helper It is used to load the helper class. We can pass the helper function in the...

6 minutes read.