2013-02-26 52 views
0

我有一個表在MySQL中包含的情侶,我想傳遞給表達式preg_replace正則表達式從MySQL

我的代碼:

$q = mysql_query("SELECT * FROM TABLE"); 
while($r=mysql_query($q)) { 
    $expressions = $r['expressions']; 
} 
$expressions = explode("\n", $expressions); 
foreach ($expressions as $expression) { 
    $content = preg_replace($expression, '', $content); 
} 

在數據庫中的一些數據:

#<div class="FBTbtn">(.*?)</div># 
#<!-- Extra Link -->(.*?)<!-- End Of Extra Link --># 
#<a(.*?)/a># 
#Learn More:# 
+0

是每一行一個數據庫條目或整個bloock是? – 2013-02-26 20:40:11

+0

整塊是。我做爆炸,然後foreach – 2013-02-26 20:52:07

回答

0

我用固定的吧 「」 而不是 「\ n」

代碼:

$q = mysql_query("SELECT * FROM TABLE"); 
while($r=mysql_query($q)) { 
    //Remove last "," from string 
    $expressions = rtrim($r['expressions'], ","); 
} 
//Explode string into array based on "," instead of "\n" 
$expressions = explode(",", $expressions); 
foreach ($expressions as $expression) { 
    $content = preg_replace($expression, '', $content); 
} 

在數據庫中的數據應該是這樣的:

#<div class="FBTbtn">(.*?)</div>#,#<!-- Extra Link -->(.*?)<!-- End Of Extra Link -->#,#<a(.*?)/a>#,#Learn More:#, 
0

如在mysql_query()函數頁面中所述,使用mysql_fetch_assoc()遍歷結果。 http://php.net/manual/en/function.mysql-query.php

$content = 'foo bar baz'; 
$result = mysql_query("SELECT * FROM expressions"); 
while ($row = mysql_fetch_assoc($result)) { 
    $content = preg_replace($row['expression'], '', $content); 
} 

順便說一下,不推薦使用mysql _ *()函數。你應該切換到PDO。 http://php.net/manual/en/book.pdo.php

+0

謝謝,但它仍然給我一個錯誤 – 2013-02-26 20:26:14

+0

什麼是錯誤?你有什麼嘗試?你是否簡單地在答案中對代碼進行了複製/過去? – mimipc 2013-02-26 20:28:45

+0

@MohamedAtef:那個錯誤是什麼? – 2013-02-26 20:32:45