2013-04-22 48 views
5

其發送到數據庫之前我解析字符串更換等值的字符串的所有地方。我想遍歷該字符串中的所有<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++; 
} 

任何想法嗎?

由於提前,

+0

那麼首先,所有的第一次迭代,你將取代''
所有實例....要使用'substr_replace'它可以讓做這種方式,你將不得不字符串的唯一replce部分你爲他替換定義位置。 – prodigitalson 2013-04-22 16:44:31

回答

6

我會使用一個正則表達式和一個自定義的回調,像這樣:

$str = "Line <br> Line <br> Line <br> Line <br>"; 
$replace = array("1", "2", "3", "4"); 
$str = preg_replace_callback('/<br>/', function($match) use(&$replace) { 
    return array_shift($replace) . ' ' . "\n"; 
}, $str); 

請注意,這是假定我們可以修改$replace陣列。如果不是的話,你可以保留一個計數器:

$str = "Line <br> Line <br> Line <br> Line <br>"; 
$replace = array("1", "2", "3", "4"); 
$count = 0; 
$str = preg_replace_callback('/<br>/', function($match) use($replace, &$count) { 
    return $replace[$count++] . ' ' . "\n"; 
}, $str); 

您可以從this demo看到這個輸出:

Line 1 Line 2 Line 3 Line 4 
+0

你回答的工作和str被正確解析,但由於某種原因,當我調用INSERT INTO時它不會被插入到數據庫中。 任何想法爲什麼? – r3x 2013-04-22 17:15:33

+0

@ r3x - 怎麼回事?你有錯誤信息嗎?如果您想要打印文字'\ n'字符而不是讓PHP將其解釋爲換行符,請將''\ n「'中的雙引號改爲單引號。 – nickb 2013-04-22 17:18:30

+0

這裏是我的插入查詢(interestTable基本上是解析的字符串)。 ($'userID','$ storyID','$ rank','$ storyType','$ interestTable')「,$ dbh1('INSERT INTO $ tbl_name(userID,storyID,rank,storyType,interestTable)VALUES );'如果我從這個查詢中刪除interestTable的值,那麼一切正常,但如果我添加它沒有發送。至於錯誤,我沒有得到任何。奇怪:S – r3x 2013-04-22 17:23:09

1
$str = str_replace('<br>', $replace[$index] . ' ' .'\n', $str); 

這是取代ALL的的<br>發現出現次數。

正確的是隻做每次迭代一個單一的更換:substr_replace可以替換字符串的一個單一的一部分。正確的應該是:

while($pos = strpos($str, '<br>') !== false) 
{ 
    $str = substr_replace($str, $replace[$replaceIndex] . ' ' .'\n', $pos, 4); // The <br> is four bytes long, so 4 bytes from $pos on. 
    $replaceIndex++; 
} 

(!不要忘了replaceIndex$$replaceIndex是一個變量)

+1

「str_replace」的第四個參數不是要多少次替換。這是一個參考,它被設置爲它*替換了多少次。 – 2013-04-22 16:52:00

0

你需要記住的$中的變量前面。此外,請嘗試使用php regex function,它允許指定替換次數。

<?php 
$str  = "Line <br> Line <br> Line <br> Line <br>"; 
$replace = array("1", "2", "3", "4"); 
$index = 0; 
$count = 1; 

while(strpos($str, '<br>') != false) 
{ 
    $str = preg_replace('/<br>/', $replace[$index] . " \n", $str, 1); //str_replace, replaces the first occurance of <br> it finds 
    $index++; 
} 
echo $str 
?> 
+0

最後一個參數不允許您設置替換次數。這是一個參考,它被設置爲它所做的替換次數。 – 2013-04-22 16:54:03

+0

我指的是[preg_replace]中的$ limit變量(http://php.net/manual/en/function.preg-replace.php)。 – George 2013-04-22 16:56:30

+0

我以爲你使用'$ count'參數。沒關係:-) – 2013-04-22 17:00:03

1

如果你只是想數它,你也可以使用它。

$str_expl = explode('<br>',$str); 
foreach($str_expl as $index=>&$str) 
    if(!empty($str)) 
     $str .= ($index+1); 
$str = implode($str_expl); 
+0

如果你想用自己的數組替換找到的項目,你可以將'str。=($ index + 1)'改成'str。= $ myArray [index];' – ikkez 2013-04-22 17:00:20

0

這是我的版本。 8格式化線,沒有正則表達式,KISS。

$str = "Line <br> Line <br> Line <br> Line <br>"; 
$replace = array("1", "2", "3", "4"); 

$temp = explode("<br>", $str); 
$result = ""; 
foreach ($temp as $k=>$v) { 
    $result .= $v; 
    if ($k != count($temp) - 1) { 
     $result .= $replace[$k]; 
    } 
} 

echo $result;