简体   繁体   English

如何在PHP中制作特定数组的首字母?

[英]How to make the first letter of specific array in PHP?

I have a simple regex that trims urls to their root domain. 我有一个简单的正则表达式,可将网址修剪到其根域。

Problem: How to make the first letter of a specific array in PHP ? 问题:如何在PHP中制作特定数组的首字母? The array output is an associative array. 数组输出是一个关联数组。 The line echo $matches[0] is the output that I need to convert the first letter to capitalize. 行echo $ matches [0]是我需要将第一个字母转换为大写的输出。

 <?php
 $pattern = '/\w+\..{2,3}(?:\..{2,3})?(?:$|(?=\/))/i';
 $url = 'http://www.test.com.uk';
 //echo $url;
 if (preg_match($pattern, $url, $matches) === 1) {
 echo $matches[0];
 }
 ?>

The code works okay except that the associative array must have a capitalize letter The output of the code above looks like this: test.com.uk 代码可以正常工作,只是关联数组必须有一个大写字母 。上面代码的输出如下所示:test.com.uk

Output: But the output I am looking for is this: Test.com.uk 输出:但是我正在寻找的输出是:Test.com.uk

Please help me. 请帮我。

在比赛中使用ucfirst()

echo ucfirst($matches[0]);

使用ucfirst函数:

echo ucfirst($matches[0]);

只需使用ucfirst php函数

echo ucfirst($matches[0]);

可能比regex更合适:

echo ucfirst(parse_url($url, PHP_URL_HOST));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM