每个单词的首字母大写,URL除外

  • 本文关键字:URL 除外 单词 php regex url
  • 更新时间 :
  • 英文 :


有人能告诉我如何做到这一点吗:

Input:
hello http://DOMAIN.com/asdakdjk.php?asd=231&adsj=23 u.s. nicely done!
Result:
Hello http://DOMAIN.com/asdakdjk.php?asd=231&adsj=23 U.S. Nicely Done!

包括中用"分隔的单词如果可能的话,例如在美国

感谢

试试这个:

<?php
function capitalizeNonURLs($input)
{
    preg_match('@(https?://([-w.]+)+(:d+)?(/([w/_.]*(?S+)?)?)?)@', $input, $matches);
    $url = $matches[1];
    $temp = ucwords($input);
    $output = str_ireplace($url, $url, $temp);
    return $output;
}
$str = "hello http://domain.com/asdakdjk.php?asd=231&adsj=23 u.s. nicely done!";
echo capitalizeNonURLs($str);

请记住,此函数不处理缩写(它不会将usa更改为usa)。国家/地区代码可以用几种不同的方式处理。一种是制作一个国家代码的哈希图,并替换它们,或者使用正则表达式。

要降低url:

$strarray = explode(' ',$str);
for($i=0;$i<count($strarray))
{
if(substr($strarray[$i],0,4)!='http')
{
    $strarray[$i] = ucfirst($strarray[$i])
}
}
$new_str = implode('',$strarray);

最新更新