×

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

Migration Class CodeIgniter

The CodeIgniter framework provides the migration class that helps the web developers to create and handle databases in a structured and streamlined manner. The migration classes are very useful when more than...

6 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 Security Class

Security Class The security class provides various function that helps to create a secure application and process input data securely to the application. This class is preloaded in the CodeIgniter application, so you...

4 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 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 Smiley Helper

The smiley helper file contains smiley images that are used in the application for showing emotions and comments while chatting with others. Load a Smiley Helper Before using Smiley Helper in the CodeIgniter application, you...

4 minutes read.

CodeIgniter Models

Models are the classes that deals with backend operations. It is used to fetch the data from the database and send it to the Controller. All the database related information provided inside...

5 minutes read.

CodeIgniter Routes

The Routes are used to handle URL requests on the browser. It matches the URL request to the predefined routes in CodeIgniter. Therefore, a file must be predefined as a routes.php file to...

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

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.

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.

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

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

Installation of CodeIgniter

Before installing CodeIgniter, you need to make sure that the wamp or xampp server in the system is preinstalled. The installation procedure for CodeIgniter is very easy. You can download CodeIgniter by following these...

2 minutes read.