其發送到數據庫之前我解析字符串更換等值的字符串的所有地方。我想遍歷該字符串中的所有<br>
,並將它們替換爲我從數組中獲得的唯一數字,然後是newLine。從一個數組
例如:
str = "Line <br> Line <br> Line <br> Line <br>"
$replace = array("1", "2", "3", "4");
my function would return
"Line 1 \n Line 2 \n Line 3 \n Line 4 \n"
聽起來很簡單。我只想做一個while循環,得到的<br>
使用strpos所有occurances,並替換那些需要的數字+ \ N使用str_replace函數。
問題是,我總是得到一個錯誤,我不知道我做錯了嗎?可能是一個愚蠢的錯誤,但仍然很煩人。
這裏是我的代碼
$str = "Line <br> Line <br> Line <br> Line <br>";
$replace = array("1", "2", "3", "4");
$replaceIndex = 0;
while(strpos($str, '<br>') != false)
{
$str = str_replace('<br>', $replace[index] . ' ' .'\n', $str); //str_replace, replaces the first occurance of <br> it finds
index++;
}
任何想法嗎?
由於提前,
那麼首先,所有的第一次迭代,你將取代''
所有實例....要使用'substr_replace'它可以讓做這種方式,你將不得不字符串的唯一replce部分你爲他替換定義位置。 – prodigitalson 2013-04-22 16:44:31