CodeSky 代码之空

随手记录自己的学习过程

PHP 删除字符串中的下划线并每个单词大写

2014-02-02 19:11分类: PHP评论: 0

题目如下: 请写一个函数,实现以下功能: 字符串“open_door”转换成“OpenDoor”、”make_by_id”转换成“MakeById”。

似乎我有点没用函数(其实封装起来就好了吧OTZ)。

演示

1<!DOCTYPE html>
2<html>
3<head>
4	<meta charset="utf-8">
5	<title>Ucword</title>
6</head>
7<body>
8	<form action="ucword.php" method="post">
9		<input type="text" placeholder="输入如open_door" name="str">
10		<input type="submit" value="发射">
11	</form>
12	<?php
13		if($_SERVER['REQUEST_METHOD'] === 'POST') {
14			$problem = FAlSE;
15
16			if (empty($_POST['str'])) {
17				$problem = TRUE;
18				echo '请输入文字呀亲';
19			}
20
21			if (!$problem) {
22				$str = $_POST['str'];
23				$str = str_replace('_', ' ', $str);
24				$str = ucwords($str);
25				$str = str_replace(' ', '', $str);
26				echo $str;
27			}
28		}
29	?>
30</body>
31</html>
32

封装成函数的好处不言而喻 不用局限于_,更具有灵活性,代码很简单,只是str_replace()ucwords()的轮流使用而已。

评论 (0)