简体   繁体   中英

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 ? 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.

 <?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

Output: But the output I am looking for is this: 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));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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