GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Revising text file from ALL CAPS to First Letter Caps.. php script? (https://gfy.com/showthread.php?t=798326)

mkx 01-09-2008 04:31 AM

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 01-09-2008 04:50 AM

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

farkedup 01-09-2008 04:52 AM

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

rowan 01-09-2008 04:56 AM

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

SmokeyTheBear 01-09-2008 05:01 AM

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

mkx 01-15-2008 05:57 AM

Quote:

Originally Posted by rowan (Post 13628117)
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 :(

rowan 01-15-2008 06:37 AM

Quote:

Originally Posted by mkx (Post 13652932)
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.

dekaz 01-15-2008 08:50 PM

Use ucwords($string);

woj 01-15-2008 09:13 PM

wtf are you guys smoking?

<?php
$file=file('somefiletxt');
$file=array_map('strtolower',$file);
$file=array_map('ucwords',$file);
echo implode('',$file);
?>


All times are GMT -7. The time now is 03:50 AM.

Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123