2012-05-16 3 views
-1

제발 도와주세요.PHP를 사용하여 3 문자로 구성된 단어를 제거하십시오

내가 PHP 코드를 사용하여 3 자 이하로 구성 단어를 제거하려면, 다음과 같은 문장 도와주세요 :

$string = "this is my new minimalist style"

는 결과가 있어야합니다 "이 새로운 미니멀 한 스타일의"

그래서 내가 작성해야하는 코드는 무엇입니까?

+0

단어 "새"3 문자가 길지 않습니까? – core1024

+1

그는 3 자보다 * 적은 *을 제거하려고합니다 – ilanco

+0

@ core1024 : 예,하지만 3 자 미만의 문자를 제거하는 것이 요구됩니다. –

답변

2

당신은 할 수 있습니다 :

$string = "this is my new minimalist style" 
$string = preg_replace(array('/\b\w{1,2}\b/','/\s+/'),array('',' '),$string); 
1

을해야한다고 :

$text = "this is my new minimalist style"; 
$text = preg_replace("/\b(\w{1,2}\s|\s\w{1,2})\b/","", $text); 
+0

forx for all ... 마침내 나는이 코드를 시도한다. $ string = "이것은 나의 새로운 미니멀리스트 스타일이다."; $ no_char = array ('is', 'my'); $ pattern = '/ \ b (?:'. join ('|', $ no_char). ') \ b/i'; $ output = preg_replace ($ pattern, '', $ string); –

0

PHP에 대한 또 다른 soution> = 5.3 (또한 익명 함수를 피할 필요 단지 것, 이전 버전과 함께 작동 사용) :

$string = "this is my new minimalist style"; 

print implode(" ", array_filter(explode(" ", $string), function($item) { return strlen($item) >= 3; })); 
관련 문제