[英]Capitalize first letter of each word pulled from mysql with PHP/jQuery
[英]Capitalize First Letter Of Each Word except for URLs
有人可以告訴我如何做到這一點:
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);
請記住,此 function 不處理縮寫(它不會將美國更改為美國)。 國家代碼可以用幾種不同的方式處理。 一種是制作國家代碼的 hashmap 並替換它們或使用正則表達式。
要保持較低的網址:
$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.