加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

如何在php中将19a史密斯街改为19A史密斯街

发布时间:2020-12-13 13:04:22 所属栏目:PHP教程 来源:网络整理
导读:我想将街道地址转换为Title Case.这不完全是Title Case,因为一串数字末尾的字母应该是大写的.例如史密斯街19号. 我知道我可以使用“19史密斯街”改为“19史密斯街” $str = ucwords(strtolower($str)) 但是将“19a史密斯街”改为“19a史密斯街”. 如何将其转
我想将街道地址转换为Title Case.这不完全是Title Case,因为一串数字末尾的字母应该是大写的.例如史密斯街19号.

我知道我可以使用“19史密斯街”改为“19史密斯街”

$str = ucwords(strtolower($str))

但是将“19a史密斯街”改为“19a史密斯街”.

如何将其转换为“19A史密斯街”?

另一种方法,更长,但可以更容易调整其他不可预见的情况,因为这是一个非常自定义的行为.
$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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读