Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php preg replace使用中的$1作为关联_Php_Mysql_Regex_Preg Replace - Fatal编程技术网

php preg replace使用中的$1作为关联

php preg replace使用中的$1作为关联,php,mysql,regex,preg-replace,Php,Mysql,Regex,Preg Replace,问题是,当使用$1作为数组的索引时,它没有显示或计算变量 Data Table fname kim bob $exp="<input type='text' name='fname' value='\"fname\"'>"; while($row=mysql_fetch_array($data)) { $dstr=preg_replace('/"([^"]+)"/', $row["$1"], $exp);

问题是,当使用$1作为数组的索引时,它没有显示或计算变量

        Data Table
        fname
        kim
        bob


$exp="<input type='text' name='fname' value='\"fname\"'>";

while($row=mysql_fetch_array($data)) {
    $dstr=preg_replace('/"([^"]+)"/', $row["$1"], $exp); 
    echo $dstr;
}
数据表
文件名
基姆
上下快速移动
$exp=”“;
while($row=mysql\u fetch\u数组($data)){
$dstr=preg_replace(“/”([^“]+)“/”,$row[“$1”],$exp);
echo$dstr;
}
这将导致一个空白文本字段

<input type='text' name='fname' value=''> x2
x2
期望结果:

<input type='text' name='fname' value='kim'><input type='text' name='fname' value='bob'>

preg\u replace的第二个参数必须是字符串。它是替换匹配字符串的模板。您不知道preg\u replace之前的模板

所以,你需要使用

大概是这样的:

$dstr=preg_replace(
    '/"([^"]+)"/', 
    function(array $matches)use($row)
        {return $row[$matches[1]];}, 
    $exp);
$exp = "<input type='text' name='fname' value='blahblah' />";
while($row = mysqli_fetch_assoc($data)){
  foreach($row as $v){
    $dstr = preg_replace('/^(.+)blahblah(.+)$/', "$1$v$2", $exp); 
    echo $dstr;
  }
}
也许你喜欢这样:

$dstr=preg_replace(
    '/"([^"]+)"/', 
    function(array $matches)use($row)
        {return $row[$matches[1]];}, 
    $exp);
$exp = "<input type='text' name='fname' value='blahblah' />";
while($row = mysqli_fetch_assoc($data)){
  foreach($row as $v){
    $dstr = preg_replace('/^(.+)blahblah(.+)$/', "$1$v$2", $exp); 
    echo $dstr;
  }
}
$exp=”“;
while($row=mysqli\u fetch\u assoc($data)){
foreach($v行){
$dstr=preg_replace('/^(+++)blahblah(+$/'),“$1$v$2”,$exp);
echo$dstr;
}
}

这不是
$1
的工作方式。它应该出现在文本替换字符串中,以供pcre用作占位符。它在PHP上下文中不起作用。这里,您的
$row
数据库结果没有名为
['$1'的字段/键
。我强烈建议改为面向对象的PHP,使用新的mysqli而不是mysql。这是一个功能示例。