我有一個字符串數組,我想插入文本。PHP foreach迭代前一個結果
$text "some text";
foreach ($arrayStrings as $string){
$new_text = preg_replace($old_string, $new_string, $text);
}
我該如何設置它,以便每個迭代將使用$ new_text的前一個結果?即最後,我得到一個文本版本,其中包括數組中的所有新字符串,而不僅僅是最後一個,這是我現在得到的。
感謝
我有一個字符串數組,我想插入文本。PHP foreach迭代前一個結果
$text "some text";
foreach ($arrayStrings as $string){
$new_text = preg_replace($old_string, $new_string, $text);
}
我該如何設置它,以便每個迭代將使用$ new_text的前一個結果?即最後,我得到一個文本版本,其中包括數組中的所有新字符串,而不僅僅是最後一個,這是我現在得到的。
感謝
我會怎麼做:
$text "some text";
$new_text = $text;
foreach ($arrayStrings as $string){
$new_text = preg_replace($old_string, $new_string, $new_text);
}
'$ new_text' - >'$ NEW_TEXT []'創建一個數組?! – Rizier123
爲什麼問題得不到解決? – user3857924
因爲它沒有意義。你迭代一個數組,但你不使用數組元素('$ string')。 –