[英]How to capitalize first letter of first word in a sentence?
I am trying to write a function to clean up user input. 我正在尝试编写一个函数来清理用户输入。
I am not trying to make it perfect. 我并不想让它变得完美。 I would rather have a few names and acronyms in lowercase than a full paragraph in uppercase. 我宁愿用小写的名字和首字母缩略词比用大写的完整段落。
I think the function should use regular expressions but I'm pretty bad with those and I need some help. 我认为该函数应该使用正则表达式,但我很糟糕,我需要一些帮助。
If the following expressions are followed by a letter, I want to make that letter uppercase. 如果下面的表达式后跟一个字母,我想把那个字母写成大写。
"."
". " (followed by a space)
"!"
"! " (followed by a space)
"?"
"? " (followed by a space)
Even better, the function could add a space after ".", "!" 更好的是,该功能可以在“。”之后添加一个空格,“!” and "?" 和“?” if those are followed by a letter. 如果那些后面跟着一封信。
How this can be achieved? 如何实现这一目标?
$output = preg_replace('/([.!?])\s*(\w)/e', "strtoupper('\\1 \\2')", ucfirst(strtolower($input)));
Since the modifier e is deprecated in PHP 5.5.0: 由于修饰符e在PHP 5.5.0中已弃用:
$output = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
return strtoupper($matches[1] . ' ' . $matches[2]);
}, ucfirst(strtolower($input)));
Here is the code that does as you wanted: 这是您想要的代码:
<?php
$str = "paste your code! below. codepad will run it. are you sure?ok";
//first we make everything lowercase, and
//then make the first letter if the entire string capitalized
$str = ucfirst(strtolower($str));
//now capitalize every letter after a . ? and ! followed by space
$str = preg_replace_callback('/[.!?] .*?\w/',
create_function('$matches', 'return strtoupper($matches[0]);'), $str);
//print the result
echo $str . "\n";
?>
OUTPUT: Paste your code! Below. Codepad will run it. Are you sure?ok
输出: Paste your code! Below. Codepad will run it. Are you sure?ok
Paste your code! Below. Codepad will run it. Are you sure?ok
Separate string into arrays using ./!/?
使用./!/?
将字符串分隔成数组./!/?
as delimeter. 作为界限。 Loop through each string and use ucfirst(strtolower($currentString))
, and then join them again into one string. 循环遍历每个字符串并使用ucfirst(strtolower($currentString))
,然后将它们再次连接到一个字符串中。
$output = preg_replace('/([\.!\?]\s?\w)/e', "strtoupper('$1')", $input)
This: 这个:
<?
$text = "abc. def! ghi? jkl.\n";
print $text;
$text = preg_replace("/([.!?]\s*\w)/e", "strtoupper('$1')", $text);
print $text;
?>
Output:
abc. def! ghi? jkl.
abc. Def! Ghi? Jkl.
Note that you do not have to escape .!? 请注意,您不必逃!? inside []. 在里面[]。
How about this? 这个怎么样? Without Regex. 没有正则表达式。
$letters = array(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
);
foreach ($letters as $letter) {
$string = str_replace('. ' . $letter, '. ' . ucwords($letter), $string);
$string = str_replace('? ' . $letter, '? ' . ucwords($letter), $string);
$string = str_replace('! ' . $letter, '! ' . ucwords($letter), $string);
}
Worked fine for me. 为我工作得很好。
$Tasks=["monday"=>"maths","tuesday"=>"physics","wednesday"=>"chemistry"];
foreach($Tasks as $task=>$subject){
echo "<b>".ucwords($task)."</b> : ".ucwords($subject)."<br/>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.