2014-02-13 61 views
0

這裏是我的一段話:如何在php中找到段落長度相同的單詞?

$string = "A country is a region legally identified as a distinct entity in political geography. 

我通過試了一下,

$word = str_word_count($string); but it returns number of words only.  

現在我想算的字母數量與長度爲2.在給定的字符串他們是3個字具有數的字母2.

它怎麼可能在PHP中?請幫我找出解決方案?

回答

1

你可以做這樣的

<?php 
$string = "A country is a region legally identified as a distinct entity in political geography."; 
$arr = explode(' ',$string); 
foreach($arr as $val) 
{ 
    if(strlen($val)==2) 
    { 
     echo "$val has a length of 2.<br>"; 
    } 
} 

OUTPUT :

is has a length of 2. 
as has a length of 2. 
in has a length of 2. 
+1

謝謝you..it是正確的解決我的問題。 –