01c10-1.php

来自「介绍PHP5的给类型函数应用」· PHP 代码 · 共 44 行

PHP
44
字号
<?php// A function to properly uppercase a titlefunction title_upcase($str) {	// First of all, uppercase all thse words using ucwords	$str = ucwords($str);		// Retreive all the words referenced by their position in the string:	$wordlist = str_word_count($str, 2);		// Remove the first and last word, since they are to remain capitalized	$wordlist = array_slice($wordlist, 1, -1, true);		// Loop over all the remaining words.	foreach ($wordlist as $position => $word) {		// If this word is one of those that should be lowercased		switch ($word) {			case 'A':			case 'An':			case 'The':			case 'But':			case 'As':			case 'If':			case 'And':			case 'Or':			case 'Nor':			case 'Of':			case 'By':				// Replace the first letter with a lowercase version:				$lower = strtolower($word);				$str{$position} = $lower{0};		}	}	// Return the formatted string	return $str;}$sample = "a study of intersteller galaxies as presented by scientists";// Uppercase a title and echo out the results.  It will output:// A Study of Intersteller Galaxies as Presented by Scientists$upcased = title_upcase($sample);echo "<pre>{$upcased}</pre>";?>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?