preg_replace和sql select的奇怪行为

时间:2016-05-16 10:36:22

标签: mysql regex foreach preg-replace preg-match-all

      preg_match_all('#\<td id=\"(.*)\"  class=\"(.*)column(.*)\>(.*)\<\/td\>#i', $htmlcontent, $matches);
  $output = $htmlcontent;
  foreach ($matches[1] as $match) {
    echo $match.",";
    $ressql = "SELECT * FROM var WHERE varimportedindex = '".$match."' AND projectid = '".$pid."' AND sheetName = '".$sheetName."'";
    $result2 = $db->query("SELECT * FROM var WHERE varimportedindex = '".$match."' AND projectid = '".$pid."' AND sheetName = '".$sheetName."'");
    $rowoperation = $result2->fetch_assoc(); //<-- HERE
    #echo $rowvalue = $rowoperation['varvalue'];
    $output = preg_replace("#\<td id=\"(.*)\"  class=\"(.*)column(.*)\>(.*)\<\/td\>#i", "<td id='\\1'  class=\"\\2column\\3\"><input type='input' id='\\1' name='\\1' value='".$rowvalue."'>\\4</td>", $output);
  }
  echo $output;

好的,我在那里找不到问题,但是如果我在这里停用替换行,一切正常。但是当我激活它时,替换不再起作用了。

有人能用这些线找到问题吗?

非常感谢你。

此致 奥拉夫

1 个答案:

答案 0 :(得分:0)

// Olaf, please edit your question to display a sample input value for $htmlcontent
if(!preg_match_all('#\<td id=\"(.*)\"  class=\"(.*)column(.*)\>(.*)\<\/td\>#i',$htmlcontent,$matches)){
    echo "<div>No match</div>";
}else{
    $where_ext=implode("' OR `varimportedindex`='",$matches[1]);
    $query="SELECT * FROM `var` WHERE projectid='{$pid}' AND `sheetName`='{$sheetName}' AND (`varimportedindex`='{$where_ext}') ORDER BY varimportedindex;";  // only run one query
    if($result=$db->query($query)){
        $pattern="#\<td id=\"(.*)\"  class=\"(.*)column(.*)\>(.*)\<\/td\>#i";
        while($row=$result->fetch_assoc()){
            echo "<div>vii={$varimportedindex} & vv={$row["varvalue"]}</div>";
            // Olaf, please state what $varvalue's value might be
            $replace="<td id='\\1' class=\"\\2column\\3\"><input type='input' id='\\1' name='\\1' value='{$row["varvalue"]}'>\\4</td>";
            $output=preg_replace($pattern,$replace,$output);
        }
        echo "<div>{$output}</div>";
        // Olaf, please edit your question to display your expected result based on your sample $htmlcontent
    }else{
        echo "<div>{$db->error}</div>";
    }
}