2012-08-06 16 views
0

我使用下面的代碼來拉一些關鍵字,並將其添加在WordPress的標籤。PHP:跳過字,如果字符計數<3

if (!is_array($keywords)) { 

    $count = 0; 

    $keywords = explode(',', $keywords); 

} 

foreach($keywords as $thetag) { 

    $count++; 

    wp_add_post_tags($post_id, $thetag); 

    if ($count > 3) break; 

} 

的代碼將只獲取4個關鍵詞,但是最重要的是,我想拉,只有當他們是超過2個字符以上,所以我不明白,只有2個字母的標籤。

有人可以幫助我。

回答

1

strlen($string)會給你字符串的長度:

if (!is_array($keywords)) { 
    $count = 0; 
    $keywords = explode(',', $keywords); 
} 

foreach($keywords as $thetag) { 
    $thetag = trim($thetag); // just so if the tags were "abc, de, fgh" then de won't be selected as a valid tag 
    if(strlen($thetag) > 2){ 
     $count++; 
     wp_add_post_tags($post_id, $thetag); 
    } 

    if ($count > 3) break; 
} 
+0

謝謝你很多val – 2012-08-06 02:55:07

1

使用strlen檢查長度。

int strlen (string $string)

返回給定的字符串的長度。

if(strlen($thetag) > 2) { 
    $count++; 
    wp_add_post_tags($post_id, $thetag); 
} 
+0

謝謝太國家文物局! – 2012-08-06 02:55:47