0

我在 MySQL 中有一个表,其中包含我想传递给的几个表达式preg_replace

我的代码:

$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:#
4

2 回答 2

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

于 2013-02-26T20:17:07.830 回答
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:#,
于 2013-02-26T22:58:07.003 回答