Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.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大写str_替换,如果在点之后_Php_Str Replace - Fatal编程技术网

PHP大写str_替换,如果在点之后

PHP大写str_替换,如果在点之后,php,str-replace,Php,Str Replace,我有一个str_替换,将[++]替换为单词He,但是如果[++]前面有一个点和空格,或者只有一个点,那么它应该是一个大写H的He,否则它应该是一个小写H的He $filterString = "[++] has performed well. [++] is showing good understanding"; echo str_replace("[++]", "Name", $filterString); 有什么建议吗 谢谢请尝试此代码,分两步进行替换 $filterString =

我有一个str_替换,将[++]替换为单词He,但是如果[++]前面有一个点和空格,或者只有一个点,那么它应该是一个大写H的He,否则它应该是一个小写H的He

$filterString = "[++] has performed well. [++] is showing good understanding";

echo str_replace("[++]", "Name", $filterString);
有什么建议吗


谢谢

请尝试此代码,分两步进行替换

$filterString = str_replace("\. [++]", ". He", $filterString);
echo str_replace("[++]", "he", $filterString);

请尝试以下代码:

$filterString = "[++] has performed well. [++] is showing good understanding";

echo str_replace(array(". [++]",".[++]","[++]"), array(". He",".He","he"), $filterString);

我们可以在str_replace中使用数组。只需检查
。[++]
[++]
首先用
He
替换,然后我们检查剩余部分并将其更改为
He
,我们实际上可以使用1
stru replace()

哪些产出:

他表现得很好。他表现出很好的理解力



旁注:另一个函数提供了您可能想要使用的有趣函数:它将字符串的第一个字符设置为大写。如果你想让每个句子的第一个字符都是大写,你可以用分隔符
分解你的输入段落并使用这个函数,然后
内爆()
用胶水把它粘回来

你能给我们看看你的输入和输出吗?编辑你的正则表达式以符合你的规范。可能类似\.[+]=>He,然后[++]=>heDo两次,一次带“[+]”和一次带“[+]”
$filterString = "[++] has performed well. [++] is showing good understanding";
echo str_replace(array('. [++]', '[++]'), array('. He', 'he'), $filterString);