×

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 Form Helper

A form helper file contains different functions that works with forms. It is used to create various functionality in a form such as dropdown, radio button, password, upload, hidden data, etc. These functionalities...

12 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 Date helper

A date helper file contains some predefined functions that work with date functions. Before using the date function in CodeIgniter, you have to load the date helper in the controller. For example: $this->load->helper(‘date’); Functions of...

12 minutes read.

CodeIgniter String Helper

The string helper file contains functions that allows separate operations with strings in CodeIgniter. Loading the String Helper Before using the string helper in the CodeIgniter application, you must load it in the controller...

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

The Helpers (functions) are stored in the Helper file. It helps CodeIgniter to perform different tasks. The helper file is the collection of many functions that is used to perform specific task. It...

3 minutes read.

Delete a record from Database CodeIgniter

Delete a record from the Database After inserting, retrieving, and updating table records, we will now learn how we can delete a particular record from a database table. To delete a record, first, we...

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

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.

Insert data to the database CodeIgniter

After successfully creating a database connection with the CodeIgniter application, we will now understand how we can insert records into the database. To insert a record into a database table, Codeigniter provides...

8 minutes read.

CodeIgniter URL Helper

The helper file contains some predefined functions that are used to work with URL, such as generate the current file path, transfer control, redirect, create a link, etc. Load the URL Helper Before using...

7 minutes read.

CodeIgniter Number helper

The number helper file contains some predefined function that deals with numeric data to display numbers in bytes format. Load a Number Helper You must load a number helper file in the controller to perform...

3 minutes read.

CodeIgniter Tutorial

What is CodeIgniter? CodeIgniter is an open-source framework for developing a dynamic web application with the use of PHP. It uses the MVC framework to develop the project, which is much...

3 minutes read.

Versions of CodeIgniter

CodeIgniter v3.1.11 (This is the current version which we will used in this tutorial)CodeIgniter v3.1.10CodeIgniter v3.1.9CodeIgniter v3.1.8CodeIgniter v3.1.7CodeIgniter v3.1.6CodeIgniter v3.1.5CodeIgniter v3.1.4CodeIgniter v3.1.3CodeIgniter v3.1.2CodeIgniter v3.1.1CodeIgniter v3.1.0CodeIgniter v3.0.6CodeIgniter...

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

Update a Record CodeIgniter

Update a record In this topic, we will learn how we can update existing records in database tables. To update a record, CodeIgniter provides an update() function used with the set()...

10 minutes read.

HTML Table Class CodeIgniter

HTML Table Class The CodeIgniter framework provides an HTML Table Class that is used to develop an auto-generated table using a defined array or result sets of the database table. Loading an HTML Table Class...

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