2015-01-26 1 views

답변

1

로 시도해보십시오

/([A-Z])\w+(?=,)/g

데모 : http://regexr.com/3a9kf

PHP 코드 :

$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France"; 
$str2="Evans, C. J."; 

preg_match_all('/([A-Z])\w+(?=,)/',$str1,$matches); 
echo implode('',$matches[0])."\n"; 

preg_match_all('/([A-Z])\w+(?=,)/',$str2,$matches); 
echo implode('',$matches[0]); 
+0

데모에서는 작동하지만 php에서는'/ g'을 어떻게 바꿔야합니까? –

+0

@vanabel PHP 코드가 추가되었습니다. – madforstrength

+0

'preg_match_all'을 참조하십시오. –

1

당신이 사용할 수있는 explode()

$str1="Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France"; 
$last_names = ''; 
$s = explode(',', $str1); 
foreach($s as $v) { 
    $n[] = explode(' ',$v); 
} 
foreach($n as $ln) { 
    $last_names .= end($ln); 
} 
echo $last_names; //EvansEbinNirenbergFrance 

preg_match()

$str = 'Evans, C. J. and Ebin, Kupper and Nirenberg, Jhon France'; 
preg_match_all('/([A-Z])\w+(?=,)/', $str, $matches); 
echo implode('',$matches[0]); //EvansEbinNirenberg 
+0

저자는'and'에 의해 합쳐졌으며, 마지막 저자는'Nirenberg, Jhon France'이며 성의 이름은'Nirenberg' –

+0

답안을 보내 주셔서 감사합니다! –