CodeIgniter Forums
Create NameCase Function - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Create NameCase Function (/showthread.php?tid=48532)



Create NameCase Function - El Forum - 01-19-2012

[eluser]jamgood96[/eluser]
I've got a few hours of Codeigniter under my belt, but I haven't yet tackled Helpers or Libraries much yet.

I would like to create a function that 'tries' to properly capitalize people's names. There is something similar for Ruby. My question is what is the best way for me to implement something like this? I'm mainly going to utilize it for processing form inputs for our customer database. The forms will be filled out by employees, and in theory should be somewhat neat, but I would really like to try and control capitalization.

Any help would be great!!


Create NameCase Function - El Forum - 01-20-2012

[eluser]InsiteFX[/eluser]
PHP.NET
ucwords



Create NameCase Function - El Forum - 01-20-2012

[eluser]jamgood96[/eluser]
[quote author="InsiteFX" date="1327046798"]PHP.NET
ucwords
[/quote]

I've seen the ucwords function before, but discovered the notes just a bit ago.

I ended up creating a helper extension of the string_helper. Not sure if I did it right, but it works so far. It's pretty simple thus far, but as obvious names creep up, I'll tweak it.

Code:
if ( ! function_exists('nameCase'))
{
function nameCase($name)
{
  $name = strtolower($name);
    $name = join("'", array_map('ucwords', explode("'", $name)));
    $name = join("-", array_map('ucwords', explode("-", $name)));
    $name = join("Mac", array_map('ucwords', explode("Mac", $name)));
    $name = join("Mc", array_map('ucwords', explode("Mc", $name)));
    return $name;
}
}



Create NameCase Function - El Forum - 01-20-2012

[eluser]Bhashkar Yadav[/eluser]
You can creat a new helper for extra functionality :

Create a file into your application/helpers directory and name it like helpername_helper.php. Define your function to perform specific task.

Call it like
Code:
$this->load->helper('helpername');
and use the helper method like

Code:
$aa = helper_method($name);