2

I am wanting the output to have the First Letter Of Each Word uppercase. Here is my code.

function random_title () 
{ 
    $quotes1 = file ("wp-content/plugins/includes/classes/quotes.txt", FILE_IGNORE_NEW_LINES);
    $quotes1 = ucwords($quotes1);
    $num = rand (0, intval (count ($quotes1) / 3)) * 3;
    return $quotes1[$num];
}

Usage is:

random_title()

This part is not working, what am I doing wrong? I get no output when I put this in, but if I take it out I do get my titles but they are lowercase as they are in the text file.

    $quotes1 = ucwords($quotes1);

Thank you for your help.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
Matt
  • 163
  • 1
  • 3
  • 15
  • you need to use ucfirst to make first letter in capitals. and you need to use it at the time of returning i think – Alive to die - Anant May 04 '15 at 21:22
  • based on `return $quotes1[$num];`, `$quotes1` is an array, but `ucwords()` is to be used on a string - [`string ucwords ( string $str ) `](http://php.net/manual/en/function.ucwords.php). You could do `return ucwords($quotes1[$num]);` – Sean May 04 '15 at 21:26

1 Answers1

6

ucwords works on a single string, not an array. Just apply it after selecting a random title:

function random_title () 
{ 
    $quotes1 = file ("wp-content/plugins/includes/classes/quotes.txt", FILE_IGNORE_NEW_LINES);
    $num = rand (0, intval (count ($quotes1) / 3)) * 3;
    return ucwords($quotes1[$num]); # Here!
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350