Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何大写句子中第一个单词的第一个字母?_Php_Regex_User Input_Text Segmentation - Fatal编程技术网

Php 如何大写句子中第一个单词的第一个字母?

Php 如何大写句子中第一个单词的第一个字母?,php,regex,user-input,text-segmentation,Php,Regex,User Input,Text Segmentation,我正在尝试编写一个函数来清理用户输入 我不想让它变得完美。我宁愿用小写字母写几个名字和首字母缩写,也不愿用大写字母写一整段 "." ". " (followed by a space) "!" "! " (followed by a space) "?" "? " (followed by a space) 我认为这个函数应该使用正则表达式,但我对正则表达式的使用非常糟糕,我需要一些帮助 如果下面的表达式后面跟一个字母,我想让这个字母大写 "." ". " (followed

我正在尝试编写一个函数来清理用户输入

我不想让它变得完美。我宁愿用小写字母写几个名字和首字母缩写,也不愿用大写字母写一整段

 "."
 ". " (followed by a space)
 "!"
 "! " (followed by a space)
 "?"
 "? " (followed by a space)
我认为这个函数应该使用正则表达式,但我对正则表达式的使用非常糟糕,我需要一些帮助

如果下面的表达式后面跟一个字母,我想让这个字母大写

 "."
 ". " (followed by a space)
 "!"
 "! " (followed by a space)
 "?"
 "? " (followed by a space)
更好的是,如果“.”、“!”和“?”后面跟一个字母,则该函数可以在它们后面添加一个空格


如何实现这一点?

使用
/!/作为delimeter。循环遍历每个字符串并首先使用
ucfirst(strtolower($currentString))
,然后再次将它们合并到一个字符串中。

以下是您想要的代码:

<?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 = preg_replace('/([\.!\?]\s?\w)/e', "strtoupper('$1')", $input)

由于PHP 5.5.0中不推荐使用修饰符e:

$output = preg_replace_callback('/([.!?])\s*(\w)/', function ($matches) {
    return strtoupper($matches[1] . ' ' . $matches[2]);
}, ucfirst(strtolower($input)));

这个怎么样?没有正则表达式

$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);
}
对我来说很好。

$Tasks=[“星期一”=>“数学”,“星期二”=>“物理”,“星期三”=>“化学”];
$Tasks=["monday"=>"maths","tuesday"=>"physics","wednesday"=>"chemistry"];

foreach($Tasks as $task=>$subject){

     echo "<b>".ucwords($task)."</b> : ".ucwords($subject)."<br/>";
}
foreach($task作为$task=>$subject的任务){ 回显“.ucwords($task)。:”.ucwords($subject)。“
”; }
幸好它与论坛无关。与其假设我在做什么,不如来点帮助怎么样?你不需要更多的信息。我清楚地解释了我要做的事情。如果你能帮忙的话,就这么做,如果不能,请去别的地方。@Dagon@Enkay请互相尊重@Dagon如果你想从@Enkay那里得到什么特别的东西,那就去问一下吧?@Enkay你的大局是什么?这将在哪里\如何进行used@Dagon:正确回答问题不需要这些信息。有趣的是。我试试看。我希望能练习一下我的正则表达式,但这可以完成工作。谢谢。你不再需要创建函数:)(@w35l3y你的代码让我感兴趣…但是如果我的字符串带有这样的标记,该怎么办?例如:
你好。这不管用!
…?在这种情况下,你能改进你的代码吗?事实上,如何在不删除它们的情况下逃逸
?谢谢:)@Zagloo,在上述情况下,要求仅具有1个独立于当前空间数的空间。在你的情况下,这是一个新的问题。考虑问一个新的,让我知道。简而言之,我没有什么好消息告诉你,因为没有任何属性的
span
很容易,但是每个属性都不同的嵌套节点呢?@w35l3y-Thx供你返回!我昨天发了一篇新帖子,有人在这里找到了解决办法:;o) 你好。我不理解正则表达式的
[.!?]
部分。未替换的点不应该与任何字符(包括“!”和“?”)匹配吗?我认为应该转义,但这个答案已经在这里出现了很多时间…@NeDark,在
[]
块中,除了
-^\]
之外的任何字符都是文本。preg\u replace():不再支持/e修饰符,请改用preg\u replace\u回调
$Tasks=["monday"=>"maths","tuesday"=>"physics","wednesday"=>"chemistry"];

foreach($Tasks as $task=>$subject){

     echo "<b>".ucwords($task)."</b> : ".ucwords($subject)."<br/>";
}