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;
好的,我在那里找不到问题,但是如果我在此处停用替换行,则一切正常。但是当我激活它时,替换不再起作用。
有人可以找到这些线的问题吗?
非常感谢。
问候,
奥拉夫
最佳答案
// 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>";
}
}
关于mysql - preg_replace和sql select的奇怪行为,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37251887/