Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 除URL外,每个单词的首字母大写_Php_Regex_Url - Fatal编程技术网

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函数