×

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

How to remove an index.php file from the Codeigniter URLs?

As we know, a URL in CodeIgniter is designed in such a way that it would be search engine and human friendly, too, rather than using a standard approach to create a...

2 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 CAPTCHA Helper

A CAPTCHA (Complete Automated Public Turning test to tell Computer and Humans Apart) helper is a function that is used in web application to check whether the user is human or machine. It...

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

Image Manipulation Codeigniter

Image Manipulation A Codeigniter provides an image manipulation class that is used for resizing, cropping, rotation, and many other manipulation tasks. This class also support three major image libraries such as GD/GD2, NetPBM, and...

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

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

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.

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

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 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 User Agent Class

User Agent Class The CodeIgniter provides a user agent class that helps to identify and collect information about the browser, mobile devices, or robots visiting your site. Besides this, it is also used...

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

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.

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 Views

A View is a presentational part which shows the information to the users. It is a complex web page which contains navbar, header, footer, sidebar, etc. A view file cannot be called directly...

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