替换Mysql获取字符串中的PHP变量
我像这样在数据库中存储动态链接替换Mysql获取字符串中的PHP变量,php,mysql,variables,Php,Mysql,Variables,我像这样在数据库中存储动态链接 http://{$site}.com/{$username} 和在PHP文件中 $site = google.com;$username = 2; 但当我从db中获取时,它不会替换为php变量,它只是作为 http://{$site}.com/{$username} 我尝试了preg\u replace('/\{\$([a-zA-Z\x7f-\xff][a-zA-Z0-9\UX7F-\xff]*)\}/e',“$$1”,$row['url']) 但它只替换m
http://{$site}.com/{$username}
和在PHP文件中
$site = google.com;$username = 2;
但当我从db中获取时,它不会替换为php变量,它只是作为
http://{$site}.com/{$username}
我尝试了preg\u replace('/\{\$([a-zA-Z\x7f-\xff][a-zA-Z0-9\UX7F-\xff]*)\}/e',“$$1”,$row['url'])代码>
但它只替换mysql获取字符串中的一个变量,其他变量保持静态
那么,是否有另一种方法可以用特定的php变量替换{}
中的所有值。您将数据作为字符串获取,因此var值不会替换
http://{$site}.com/{$username}
// not varable {$site} and {$username}
这是一根绳子
请尝试插入完整url或重试
输出时只需使用:-
$site
不是有效字符串。也许您应该首先启用错误报告功能。$例如,site只是告诉您我的问题我必须一次又一次地添加str\u replace来替换所有变量是否有其他方法可以一次替换所有变量。preg\u replace立即变为variable,但之后停止,我不知道为什么你的数据填充循环中的stry str\u replace bro str\u replace会一次替换一件东西,我想让它动态化,而不是一次一件地全部替换,然后循环遍历数据,并在必要时进行str\u replace$select=mysqli_查询($conn,“从设置中选择url”);而($row=mysqli_fetch_assoc($select))$url=str_replace(数组('{$site}','{$username}'),数组($site,$username),$row['url']);
$tmpurl = str_replace('{$site}', $site, $row['url']);
$url = str_replace('{$username}', $username, $tmpurl);