2010-06-10 2 views

답변

1

당신은 공백으로 문자열을 분할하고 각 단어 얻을 수있는 explode 기능을 사용할 수 있습니다 :

$words = 'word1 word2 word3 word4 text one test four five'; 
$words_array = explode(' ', $words); 

을 그리고 당신은 단어의 수를 얻을 수있는 array_chunk 기능을 사용할 수 있습니다 :

print_r(array_chunk($words_array, 4, true)); 
7

MySQL SUBSTRING INDEX 기능을 사용하십시오.

-- Will select everything up until the fifth space. 
SELECT SUBSTRING_INDEX(YourTextField, ' ', 5); 
0

정규식 표현이 항상 좋은 때문에 :

if (preg_match('/([^\\s]*(?>\\s+|$)){0,4}/', $string, $matches)) { 
    //result in $matches[0] 
} 
관련 문제