Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/277.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.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中将19a smith STREET更改为19a smith STREET_Php - Fatal编程技术网

如何在php中将19a smith STREET更改为19a smith STREET

如何在php中将19a smith STREET更改为19a smith STREET,php,Php,我想把一个街道地址转换成类似标题格的东西。它不完全是标题大小写,因为数字字符串末尾的字母应该是大写的。史密斯街19A号 我知道我可以使用以下命令将“史密斯街19号”更改为“史密斯街19号” 但这将“19a史密斯街”转换为“19a史密斯街” 如何将其转换为“19A Smith Street”?这里有一条使用正则表达式可以走的路线 $str = '19a smith STREET'; echo preg_replace_callback('~(\d+[a-z]?)(\s+.*)~', functio

我想把一个街道地址转换成类似标题格的东西。它不完全是标题大小写,因为数字字符串末尾的字母应该是大写的。史密斯街19A号

我知道我可以使用以下命令将“史密斯街19号”更改为“史密斯街19号”

但这将“19a史密斯街”转换为“19a史密斯街”


如何将其转换为“19A Smith Street”?

这里有一条使用正则表达式可以走的路线

$str = '19a smith STREET';
echo preg_replace_callback('~(\d+[a-z]?)(\s+.*)~', function ($matches) {
            return strtoupper($matches[1]) . ucwords(strtolower($matches[2]));
        }, $str);
输出:

史密斯街19A号

正则表达式演示:

PHP演示:

您可以将行拆分为两个子字符串,分别格式化每一半,然后将这两个子字符串重新组合在一起

$str = '19a smith STREET';
$split = strpos($str, ' ');
$str1 = strtoupper(substr($str, 0, $split + 1));
$str2 = ucwords(strtolower(substr($str, $split + 1)));
$str = $str1 . $str2;
echo $str;
结果:史密斯街19A号


PHP演示:

另一种方法,时间更长,但由于这是一种非常定制的行为,对于其他不可预见的场景可能更容易调整

$string = "19a smith STREET";

// normalize everything to lower case
$string = strtolower($string);

// all words with upper case
$string = ucwords($string);

// replace any letter right after a number with its uppercase version
$string = preg_replace_callback('/([0-9])([a-z])/', function($matches){
    return $matches[1] . strtoupper($matches[2]);
}, $string);

echo $string;
// echoes 19A Smith Street

// 19-45n carlsBERG aVenue  ->  19-45N Carlsberg Avenue

根据Juank的回答,我最终使用了

     $str = preg_replace_callback('/([0-9])([a-z])/', function($matches){
           return $matches[1] . strtoupper($matches[2]);
      }, ucwords(strtolower($str))); 

我不认为有一个预定义的功能。你可以用正则表达式做点什么。尽管这取决于输入是否一致。尝试一下,您可以将行拆分为两个子字符串,格式化后半部分,然后将这两个子字符串重新组合在一起。
     $str = preg_replace_callback('/([0-9])([a-z])/', function($matches){
           return $matches[1] . strtoupper($matches[2]);
      }, ucwords(strtolower($str)));