2017-05-10 73 views
1

我已經爆炸了使用PHP的文本文件。我的目標是檢查每個字符串中的一個值,而如果「爆炸串」中存在的值,然後重新分配值:如何檢查所有的PHP爆炸的字符串

價值,我檢查:「AM,B,L,PM」 目標:轉換的 「AM,B,L,PM」 所有分解字符串這個「AM,B,L」

僞邏輯:

if ($pieces[2] = "AM,B,L,PM") 
{ 
    $pieces[2] = "AM,B,L" 
} 

上面的代碼適用於單個$餡餅CES [2],但我要檢查所有$件,$件[3],$件[4],$件[5]

請告知

+0

做一個FOR循環響鈴嗎? – RiggsFolly

+0

@RiggsFolly啞巴的問題,但你能告訴我怎麼樣 – Mike113

+7

是的。這是___非常祕密的地方,你可以看看這些東西_ [它被稱爲手冊](http://php.net/manual/en/control-structures.for.php)___Ssshhhhhh不告訴任何人有關它_ – RiggsFolly

回答

1

嘗試這個

$l = count($pieces); 

for($x=0;$l<$x;$x++) { 

    if ($pieces[$x] == "AM,B,L,PM") { 
     $pieces[$x] = "AM,B,L"; 
    } 

} 

$l = count($pieces); 

for($x=0;$l<$x;$x++) { 

    $a = preg_replace("/[^a-zA-Z]/", "", $pieces[$x]); 

    if ($a == "AMBLPM") { 
     $pieces[$x] = "AM,B,L"; 
    } 

} 
+0

一個好的答案包含一個_explanation_,而不僅僅是「嘗試這個」(這不會幫助OP理解發生了什麼)。 –

+0

Ohh Sry Bro,反正Thankyou ... – Subi

+0

當你設定這個值時,你必須在'if'(比較)和'='中有2個'=='。現在你實際上沒有設置任何東西。 –

0

你可以循環您的陣列與foreach,如果你的條件是true,分配值添加到原始數組中,最後再次將您的數組轉換爲string

foreach ($pieces as $key => $piece) { 
    if (trim($piece) == "AM,B,L,PM") { 
     $pieces[$key] = "AM,B,L"; 
    } 
} 

// In the first parameter you put the delimiter you used to explode the string 
$string = implode(' ', $pieces); 
0

$ string = str_replace('AM,B,L,PM','AM,B,L',$ pieces);

echo $ string [370];

已解決。謝謝大家