Welcome Guest, Not a member yet? Register   Sign In
A String Helper Extension for `Capitalize` and `Start With` Methods
#1

[eluser]SpYk3[/eluser]
This nice little extension has been very handy over time with some very simple request.

The `capitalize` method was originally taken from a PHP Doc posting. I've since molded into something a little more intuitive and suiting to modern day spelling of American English words, titles, names, and phrases. A second, `optional`, parameter is a Boolean for "is name". If you're seeking to capitalize a name, then pass `TRUE`, else, just leave it be.

- capitalize('jd mckinstry', TRUE); // returns JD McKinstry
- capitalize('merry christmas and happy holidays!')); // returns Merry Christmas and Happy Holidays!

The second and third methods simply help to find if a string "starts" or "ends" with a specific "needle".

- startsWith('billy bob', 'bob'); // returns bool(false)
- startsWith('billy bob', 'billy'); // returns bool(true)

- endsWith('billy bob', 'bob'); // returns bool(true)
- endsWith('billy bob', 'billy'); // returns bool(false)

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

if (!function_exists('capitalize')) {
function capitalize($str, $is_name=FALSE) {
  $str = trim(stripcslashes($str));
  
  // exceptions to standard case conversion
  if ($is_name) {
   $all_uppercase = '';//'Aj|Jd';
   $all_lowercase = 'De La|De Las|Der|Van De|Van Der|Vit De|Von|Or|And|Van ';
  }
  else { // addresses, essay titles ... and anything else
   $all_uppercase = 'Po|Rr|Se|Sw|Ne|Nw';
   $all_lowercase = 'A|And|As|By|In|Of|Or|To';
  }
  
  $prefixes = 'Mc';
  $suffixes = "'S";
  
  // captialize all first letters
  $str = preg_replace('/\\b(\\w)/e', 'strtoupper("$1")', strtolower(trim($str)));
  
  if ($all_uppercase) { // capitalize acronymns and initialisms e.g. PHP
   $str = preg_replace("/\\b($all_uppercase)\\b/e", 'strtoupper("$1")', $str);
  }
  
  if ($is_name) {
   if (strpos($str, " ") !== FALSE) {
    $ara = explode(" ", $str);
    foreach ($ara as $k => $v) {
     if ($k != count($ara)-1 && !preg_match("/[aeiouyAEIOUY]/", $v)) $ara[$k] = strtoupper($v);
    }
    $str = implode(" ", $ara);
   }
   elseif (!preg_match("/[aeiouy]/", $str)) {
    $str = strtoupper($str);
   }
  }
  
  if ($all_lowercase) { // decapitalize short words e.g. and
   if ($is_name) { // all occurences will be changed to lowercase
    $str = preg_replace("/\\b($all_lowercase)\\b/e", 'strtolower("$1")', $str);
   }
   else { // first and last word will not be changed to lower case (i.e. titles)
    $str = preg_replace("/(?<=\\W)($all_lowercase)(?=\\W)/e", 'strtolower("$1")', $str);
   }
  }
  
  if ($prefixes) { // capitalize letter after certain name prefixes e.g 'Mc'
   $str = preg_replace("/\\b($prefixes)(\\w)/e", '"$1".strtoupper("$2")', $str);
  }
  
  if ($suffixes) { // decapitalize certain word suffixes e.g. 's
   $str = preg_replace("/(\\w)($suffixes)\\b/e", '"$1".strtolower("$2")', $str);
  }
  
  // Mac Exceptions
  if (strpos($str, "Macd") === FALSE || strpos($str, "Macv") === FALSE) {
   $str = preg_replace("/Macd/", 'MacD', $str);
   $str = preg_replace("/Macv/", 'MacV', $str);
  }
  
  return trim(stripcslashes($str));
}
}

if (!function_exists('startsWith')) {
function startsWith($haystack, $needle) {
  return !strncmp($haystack, $needle, strlen($needle));
}
}

if (!function_exists('endsWith')) {
function endsWith($haystack, $needle) {
  return substr($haystack, -strlen($needle))===$needle;
}
}
#2

[eluser]jairoh_[/eluser]
you are a helper. you might wanna upload that on github sir Big Grin




Theme © iAndrew 2016 - Forum software by © MyBB