输入字符串:“任何字符串或数字字符都出现在这里。
在员工值中插入public.scheduled_charges_id_seq( 'public.scheduled_charges_id_seq‘),'shrenik',555,NULL,253,'Rahul')这是我的字符串“
所需输出:“此处显示任何字符串或数字字符。
在员工值中插入(附件( 'public.scheduled_charges_id_seq‘)、' XXX’、XXX、NULL、XXX、'XXX')
这是我的绳子“
我试过:(0-9\‘.*\’)它必须匹配给定字符串中的"insert into“,想要替换字符串中的机密值,从插入到模式应该限制为从插入到雇员的大括号(直到结束位置)。你能帮帮我吗?提前感谢
发布于 2014-02-11 06:14:09
假设insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),是固定的,并且在查询中没有任何其他函数调用或子查询,那么您可以这样做:
// Set up test string
$statement = "Any string or numeric character appears here
insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'shrenik', 555, NULL,253,'Rahul') This is my String";
$pieces = preg_split('/\)[, ]/', $statement);
// print_r($pieces);
// $pieces[0] contains everything up to "... id_seq' "
// $pieces[1] contains all of the fields you want to redact
// $pieces[2] contains everything after the closing )
$pieces[1] = preg_replace('/\'\w+\'|\d+/', 'XXX', $pieces[1]);
// print $pieces[1] . "\n";
$statement = $pieces[0] . ")," . $pieces[1] . ") " . $pieces[2];
print $statement . "\n";输出:
Any string or numeric character appears here
insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),XXX, XXX, NULL,XXX,XXX) This is my String编辑:回应OP关于不使用preg_split__的评论。
您可以使用使用频率较低的preg_replace_callback,它以一个函数回调作为其第二个参数,该回调处理匹配数组以生成所需的替换字符串。关于preg_replace_callback的文档可以是在这里发现的。
$statement = "Any string or numeric character appears here
insert into employees values( nextval( 'public.scheduled_charges_id_seq' ),'shrenik', 555, NULL,253,'Rahul') This is my String";
$statement =
preg_replace_callback('/(insert into(?:[^\)]+)\),)(.*)\) /',
function($matches) {
return $matches[1] .
preg_replace('/(^|,)[ ]*[^,]+/', '$1 XXX', $matches[2]) .
') ';
},
$statement);
print $statement . "\n";输出与前面示例中的输出相同。
以上两个示例替换了insert 查询字符串.中的每个字段。
**编辑:**添加了下面的代码示例,以响应OP的评论。
如果您想要排除某些被替换的术语(例如,NULL和NOW()),那么事情就变得复杂了。您可以像下面这样做,定义一个常量,它是一个由管道分隔的字符串,您不希望用XXX替换它。
$original_statement =
"insert into public.scheduled_charges VALUES ( nextval( 'public.scheduled_charges_id_seq' ), 4633, 9085042, 116164, 3, NULL, NULL, NULL, '055 ~..~00~..~140~..~7~..~RNT~..~Jan 1 2014 12:00AM', 875, NULL, 1, '01/01/2014', '12/31/2013', '01/01/2014', NULL, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, NULL, '01/01/2014', NULL, '01/31/2014', NULL, NULL, 2414, NOW(), 2414, NOW() ) RETURNING id;";
define('IGNORE_TERMS', 'NULL|NOW()');
$replaced_statement =
preg_replace_callback('/(insert into(?:[^\)]+)\),)(.*)[ ]*\) /',
function($matches) {
return $matches[1] .
preg_replace_callback('/(^|,)([^,]+)/',
function($matches) {
$matches[2] = trim($matches[2]);
if (in_array(strtoupper($matches[2]),
explode('|', IGNORE_TERMS))) {
return $matches[1] . ' ' . $matches[2];
}
else {
return $matches[1] . ' XXX';
}
},
$matches[2]) .
') ';
},
$original_statement);
print $original_statement . "\n";
print "\n";
print $replaced_statement . "\n";这是有效的,并给你的结果,你正在寻找。--我并不是说代码很漂亮,甚至不是非常灵活。但是,在这种情况下,它可以为您完成任务。
https://stackoverflow.com/questions/21693871
复制相似问题