tags:

views:

56

answers:

3

Example in my database SMK SUNGAI PUNAI

$school = 'SMK SUNGAI PUNAI';
echo ucwords(strtolower($school));

Ouput Smk Sungai Punai

Question

How to make the output will be SMK Sungai Punai which the SMK still in uppercase.

Update.

The problem I have 10K list of school name. From PDF I converted to mysql. I copied exactly from PDF the name of schools. All in uppercase. I need a solution if can be done.

A: 

There's no really good way to do it. In this case you can assume it's an abbreviation because it's only three letters long and contains no vowels. You can write a set of rules that look for abbreviations in the string and then uppercase them, but in some cases it'll be impossible... consider "BOB PLAYS TOO MUCH WOW."

no
+3  A: 

As far as I understand you want to have all school names with the first character of every word in uppercase and exclude some special words ($exceptions in my sample) from this processing.

You could do that like this:

   function createSchoolName($school) {
      $exceptions = array('SMK', 'PTS', 'SBP');
      $result = "";
      $words = explode(" ", $school);
      foreach ($words as $word) {
          if (in_array($word, $exceptions))
              $result .= " ".$word;
          else
              $result .= " ".strtolower($word);
      }
      return trim(ucwords($result));
   }

$school = 'SMK SUNGAI PUNAI';
echo createSchoolName($school, $exceptions);

This example would return SMK Sungai Punai as required by your question.

codescape
Thanks codescape. It's worked like a super hero.
kampit
A: 

You can use something like this:

<?php
$str = 'SMK SUNGAI PUNAI';
$str = strtolower($str);
$arr = explode(" ", $str);

$new_str = strtoupper($arr[0]). ' ' .ucfirst($arr[1]). ' ' .ucfirst($arr[2]);
echo '<p>'.$new_str.'</p>';

// Result: SMK Sungai Punai
?>
Vasil Dakov