Revising text file from ALL CAPS to First Letter Caps.. php script?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mkx
    Confirmed User
    • Nov 2003
    • 4001

    #1

    Revising text file from ALL CAPS to First Letter Caps.. php script?

    Another PHP question. I have a list of 10,000+ phrases line by line in a text file. I need to change it from:

    THE BOY
    JOHNS BIRD
    FRED SUCKS
    EAT ME

    etc etc
    to:

    The Boy
    Johns Bird
    Fred Sucks
    Eat Me

    etc etc

    Anyone know a php code or easy program/way to do this?
  • farkedup
    Confirmed User
    • Nov 2007
    • 2490

    #2
    $file = str_replace("A","a","$file");
    $file = str_replace("B","b","$file");

    go all the way to Z... thats really the easiest way that I can think of... obviously theres others but this is a quick way. I've been up all night coding so can't think of a "cleaner" way to do it...
    -- QUOTE ME IT MAKES ME FEEL SPECIAL --

    Comment

    • farkedup
      Confirmed User
      • Nov 2007
      • 2490

      #3
      just noticed that you want to keep the caps after a space... alright, that complicates it a bit more... but str_replace can do that too.... just have a space as part of the syntax
      -- QUOTE ME IT MAKES ME FEEL SPECIAL --

      Comment

      • rowan
        Too lazy to set a custom title
        • Mar 2002
        • 17393

        #4
        Code:
        $db = file("filename.txt");
        $i = 0;
        while (isset($db[$i])) {
          $blah = explode(" ", strtolower(trim($db[$i])));
          $j = 0;
          while (isset($blah[$j])) {
            $t = $blah[$j];
            $t[0] = strtoupper($t[0]);
            $blah[$j] = $t;
            $j++;
          }
          echo implode(" ", $blah) . "\n";
          $i++;
        }
        It's late, it's messy, but it should work

        Comment

        • SmokeyTheBear
          ►SouthOfHeaven
          • Jun 2004
          • 28609

          #5
          Code:
          <?php
          function custom_ucwords($map, $string) {
          
             $inside_word = TRUE;
          
             for ($index = 0; $index < strlen($string); ++$index) {
          
          
                $is_chr = isset($map[$string[$index]]);
          
          
                if (! $inside_word && $is_chr) {
                    $string[$index] = $map[$string[$index]];
                }
          
                $inside_word = $is_chr;
             }
          
             return $string;
          }
          
          
          $map = array(
             'a' => 'A', 'b' => 'B', 'c' => 'C', 'd' => 'D',
             'e' => 'E', 'f' => 'F', 'g' => 'G', 'h' => 'H',
             'i' => 'I', 'j' => 'J', 'k' => 'K', 'l' => 'L',
             'm' => 'M', 'n' => 'N', 'o' => 'O', 'p' => 'P',
             'q' => 'Q', 'r' => 'R', 's' => 'S', 't' => 'T',
             'u' => 'U', 'v' => 'V', 'w' => 'W', 'x' => 'X',
             'y' => 'Y', 'z' => 'Z'
          );
          
          $string = file_get_contents("file.txt");
          echo custom_ucwords($map, $string);
          ?>
          save as do.php

          put words in a file called file.txt

          visit php ina browser
          hatisblack at yahoo.com

          Comment

          • mkx
            Confirmed User
            • Nov 2003
            • 4001

            #6
            Originally posted by rowan
            Code:
            $db = file("filename.txt");
            $i = 0;
            while (isset($db[$i])) {
              $blah = explode(" ", strtolower(trim($db[$i])));
              $j = 0;
              while (isset($blah[$j])) {
                $t = $blah[$j];
                $t[0] = strtoupper($t[0]);
                $blah[$j] = $t;
                $j++;
              }
              echo implode(" ", $blah) . "\n";
              $i++;
            }
            It's late, it's messy, but it should work
            This worked but it puts it all on the same line. I am a donkey so I don't know how to create a new line in php for this script. Tried <br> and another \n but nothing

            Comment

            • rowan
              Too lazy to set a custom title
              • Mar 2002
              • 17393

              #7
              Originally posted by mkx
              This worked but it puts it all on the same line. I am a donkey so I don't know how to create a new line in php for this script. Tried <br> and another \n but nothing
              If you're running it as a WEB server script (ie loading blah.php with your browser) then that's easy - view the source of the page

              If not then replacing \n with <br>\n on the third last line should do the trick.

              Comment

              • dekaz
                Confirmed User
                • Mar 2003
                • 187

                #8
                Use ucwords($string);
                icq 214 - 625 - 815

                Comment

                • woj
                  <&(©¿©)&>
                  • Jul 2002
                  • 47880

                  #9
                  wtf are you guys smoking?

                  <?php
                  $file=file('somefiletxt');
                  $file=array_map('strtolower',$file);
                  $file=array_map('ucwords',$file);
                  echo implode('',$file);
                  ?>
                  Custom Software Development, email: woj#at#wojfun#.#com to discuss details or skype: wojl2000 or gchat: wojfun or telegram: wojl2000
                  Affiliate program tools: Hosted Galleries Manager Banner Manager Video Manager
                  Wordpress Affiliate Plugin Pic/Movie of the Day Fansign Generator Zip Manager

                  Comment

                  Working...