2012-02-10 38 views
5

考慮以下字符串:將字符串分割爲雙克,忽略某些標籤

I have had the greatest {A} {B} day yesterday {C} 

我想創建與雙克的陣列,忽略所有標籤(標籤{括號}之間)

[0] => I-have 
[1] => have-had 
[2] => had-the 
[3] => the-greatest 
[4] => greatest-day 
[5] => day-yesterday 

在PHP中,執行此操作的最佳方法是什麼?使用正則表達式或爆炸「」,然後遍歷所有單詞?我無法在這裏開始,所以任何幫助,將不勝感激:)

回答

2

使用explode使得它很容易的:

$string="I have had the greatest {A} {B} day yesterday {C}"; 

$words=explode(" ",$string); 

$filtered_words=array(); 

foreach($words as $w) 
{ 
    if(!preg_match("/{.*}/",$w)) 
    { 
    array_push($filtered_words,$w); 
    } 
} 


$output=array(); 

foreach(range(0,count($filtered_words)-2) as $i) 
{ 
    array_push($output,$filtered_words[$i] . "-" . $filtered_words[$i+1]); 
} 

var_dump($output); 

輸出是:

array(6) { 
    [0]=> 
    string(6) "I-have" 
    [1]=> 
    string(8) "have-had" 
    [2]=> 
    string(7) "had-the" 
    [3]=> 
    string(12) "the-greatest" 
    [4]=> 
    string(12) "greatest-day" 
    [5]=> 
    string(13) "day-yesterday" 
} 
1

略有不同的方法:

$string = '{D} I have had the greatest {A} {B} day yesterday {C}'; 

// explode on spaces 
$arr = explode(' ', $string); 
$bigrams = array(); 

// remove all "labels" with regex (assuming it matches \w) 
$arr = array_values(array_filter($arr, function($s){ 
    return !preg_match("/\{\w\}/", $s); 
})); 

// get the bigrams 
$len = count($arr); 
for ($i = 0; $i <= $len - 2; $i++) { 
    $bigrams[] = $arr[$i] . '-' . $arr[$i+1]; 
} 

print_r($bigrams); 
相關問題