Php 除URL外,每个单词的首字母大写
有人能告诉我怎么做吗:Php 除URL外,每个单词的首字母大写,php,regex,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! 如果可能,包括以“.”分隔的单词,如在美国 感谢降低URL: $strarray = explode(' ',$str); for($i=
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!
如果可能,包括以“.”分隔的单词,如在美国
感谢降低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);
$strarray=explode(“”,$str);
对于($i=0;$i请尝试以下方法:
<?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);
对于用空格分隔的单词,可以使用php的ucwords函数