Preg_replace 工作不正常
Preg_replace is not working correctly
这是我的字符串:
$tmpl = "...TABLE __? (`id` int(11...."
这里是 preg_replace:
$tmpl = preg_replace('/([^"\'0-9a-z_])__([a-z_]+[^"\'])/i', "$1".$this->config->db_prefix2."$2", $tmpl);
顺便说一句 $this->config->db_prefix2 等于 "list_ "
但是当代码执行时,preg_replace 什么都不做(保持一切不变)。在我看来,问题出在 preg_replace 正则表达式中。这个想法是
代替 __?列出_?
我不知道为什么你有这么复杂的模式,你可以这样做
$tmpl = str_replace('__?', $this->config->db_prefix2, $tmpl);
或者如果你必须使用 preg_replace
$tmpl = string_replace('/__\?/', $this->config->db_prefix2, $tmpl);
你可以在这里测试
https://regex101.com/r/pufIoY/1
也别怪你漏了table名字
$tmpl = "...TABLE __? (id
int(11...."
就是说,如果我正在制作模板,我会使用类似这样的东西
CREATE TABLE {DbPrefix}.{Table} ( id int(11...."
然后只需使用,str_replace('{DbPrefix}', $prefix )
。主要是因为它比 __?
更具可读性。但这是你的派对。
这是我的字符串:
$tmpl = "...TABLE __? (`id` int(11...."
这里是 preg_replace:
$tmpl = preg_replace('/([^"\'0-9a-z_])__([a-z_]+[^"\'])/i', "$1".$this->config->db_prefix2."$2", $tmpl);
顺便说一句 $this->config->db_prefix2 等于 "list_ "
但是当代码执行时,preg_replace 什么都不做(保持一切不变)。在我看来,问题出在 preg_replace 正则表达式中。这个想法是 代替 __?列出_?
我不知道为什么你有这么复杂的模式,你可以这样做
$tmpl = str_replace('__?', $this->config->db_prefix2, $tmpl);
或者如果你必须使用 preg_replace
$tmpl = string_replace('/__\?/', $this->config->db_prefix2, $tmpl);
你可以在这里测试
https://regex101.com/r/pufIoY/1
也别怪你漏了table名字
$tmpl = "...TABLE __? (
id
int(11...."
就是说,如果我正在制作模板,我会使用类似这样的东西
CREATE TABLE {DbPrefix}.{Table} ( id int(11...."
然后只需使用,str_replace('{DbPrefix}', $prefix )
。主要是因为它比 __?
更具可读性。但这是你的派对。