【问题标题】:Strange behaviour of preg_replace and sql selectpreg_replace 和 sql select 的奇怪行为
【发布时间】:2016-09-12 02:57:12
【问题描述】:
      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;

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

有人能找到这些行的问题吗?

非常感谢。

问候, 奥拉夫

【问题讨论】:

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


    【解决方案1】:
    // 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>";
        }
    }
    

    【讨论】:

    • @OlafK。我不确定我是否通过改进您的一般代码结构来解决您的问题。如果没有,您需要在问题中提供更多信息,请阅读我的代码中的 cmets。让我们一起努力为这个问题找到一些解决方案。更重要的是,如果您不再关心这个问题,请将其删除,以免 SO 用户浪费任何时间来帮助您。
    猜你喜欢
    • 2013-02-20
    • 2013-06-26
    • 1970-01-01
    • 2016-06-24
    • 2011-08-26
    • 2016-08-07
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多