문자열 내에서 사람 이름의 모든 인스턴스를 대문자로 표시하는 함수를 작성하려고합니다. 이름이 변경 될 수 있도록 동적이어야합니다. 예를 들면
"Hello Mr harrison and Mr jones"는 "Hello Mr Harrison and Mr Jones"가됩니다.
글자를 세는 것보다 효율적인 방법이 있습니까?
편집 : 명확히하기 위해 데이터베이스에서 4-5 개의 문장을 얻을 것입니다. 이것은 회사 엔지니어가 남긴 간단한 설명입니다. 아래 함수는 텍스트의 서식을 지정하므로 전체 대문자로 쓰든 대문자없이 쓰든 상관 없습니다. 그러나 Mr. (당시 사람의 이름)의 사례를 찾아서 수정해야합니다.
예-엔지니어가 "MR JONES FRIDGE를 수정했습니다. MR JONES WAS HAPPY WITH THE SERVICE"라는 의견을 남긴 경우-다음으로 변환됩니다.
"나는 Mr Jones 냉장고를 고쳤습니다. Mr Jones는 서비스에 만족했습니다."
편집 : 이것은 지금까지 텍스트 형식을 지정하는 내 기능입니다.
function formatTextCase($string)
// Format a string to correct upper and lower case E.g. "HELLO. goodbye." "to Hello. Goodbye."
{
// Trime whitespace
$string = trim($string);
// First format to all lower, and capitalise first letter
$string = ucfirst(strtolower($string));
// Capitalise any letter after full stop.
$string = preg_replace_callback('/[.!?:;].*?\w/', create_function('$matches', 'return strtoupper($matches[0]);'), $string);
// Test - please remove
//$string .= " Hello i am testing.";
// Replace any lower case I
$string = str_replace(" i ", " I ", $string);
// Now locate and capitalise any name after "Mr" eg. Mr jones to Mr Jones
What do I do here?
return $string;
}
감사!
여기 내가 당신을 위해 수고한 뜨거운 것이 있습니다.
function uppers($m){
$edge_cases=['De ','Di ','Du ','De La ','Van '];
$correction=['de ','di ','du ','de la ','van ']; // reverse some fringe case names
return str_replace($edge_cases,$correction,ucwords($m[0],"-' ")); // notice 2nd param of ucwords()
}
$sentences="i have fixed mr jones fridge. mrs jones was happy with the service. miss johnson-taylor told ms o'neil that everything was fabulous. mr van larsen was less than impressed but that was a missunderstanding. another person liked us on facebook!";
$sentences=preg_split('/[.!?]\K\s+/',$sentences); // generate array of sentences
$sentences=preg_replace_callback('/\bm(?:iss|r?s?)\b (?:de |di |du |de la |van )?[a-z]+[-\']?[a-z]*/','uppers',$sentences); // match full names
$sentences=implode(' ',array_map('ucfirst',$sentences)); // capitalize start of sentences and rejoin sentences into a string
echo $sentences;
산출:
I have fixed Mr Jones fridge. Mrs Jones was happy with the service. Miss Johnson-Taylor told Ms O'Neil that everything was fabulous. Mr van Larsen was less than impressed but that was a missunderstanding. Another person liked us on facebook!
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다