简体   繁体   English

正则表达式单引号仅在select和from之间,不更改开始和结束引号

[英]regex single quotes just between select and from without changing start and end quotes

i'm using php and i want to replace single quotes in a oracle sql query. 我正在使用PHP,并且我想替换Oracle SQL查询中的单引号。

i have something like this : 我有这样的事情:

select 'this is test' for 'stack overflow'' , 'another' single quote was around here' from testtable where id='test' and another='test2'

i am using oracle so the escape character is double single quote. 我正在使用oracle,因此转义字符是双单引号。

my issue is to find single quotes just only between select and from statement, replacing shouldn't remove the first single quote at start and end of each field. 我的问题是仅在selectfrom语句之间找到单引号,替换不应该删除每个字段开头和结尾的第一个单引号。

answer should be something like this 答案应该是这样的

select 'this is test'' for ''stack overflow''' , 'another'' single quote was around here' from testtable where id='test' and another='test2'

the number of fields can be vary. 字段数可以变化。 in my example it had two field each one contain single quote. 在我的示例中,它有两个字段,每个字段包含一个单引号。 but sometimes fields contain quote and sometimes not, sometimes they come after each other and some times not. 但有时字段包含引号,有时不包含引号,有时它们彼此相邻,有时则不然。

i tired different regexp. 我累了不同的正则表达式。 try to come up with a solution but no luck 尝试提出解决方案,但没有运气

so my question is how do i implement this? 所以我的问题是我该如何实现呢? which php funciton or what regex? 哪个PHP函数或什么正则表达式?

I don't think you can do what you want directly. 我认为您不能直接做您想做的事。 If you for example have 'field one', 'field' two' it could be interpreted as either 'field one', 'field'' two' or 'field one '', ''field'' two' . 例如,如果您具有'field one', 'field' two'则可以将其解释为'field one', 'field'' two''field one '', ''field'' two'

You might want to try something like this instead, if possible: 如果可能,您可能想尝试这样的事情:

//First put the field names in an array $fields
...

//Then you create the query string like this
$sql = "select '" . implode("', '", str_replace("'", "''", $fields) ) . "' from testtable where id='test' and another='test2'";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM