Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.3k views
in Technique[技术] by (71.8m points)

求一个php正则表达式将字符串之间的部分替换成其他

求一个正则表达式,用来替换字符串

$str = 'SELECT userid, username FROM t WHERE user_lock=1';

将SELECT和FROM之间的部分替换成COUNT(*)

$newstr = 'SELECT COUNT(*) FROM t WHERE user_lock=1';

SELECT和FROM可能是大写或小写

userid, username不固定也可能是其他的字符串

$str = 'SELECT id,other FROM t WHERE user_lock=1';


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

我JS这么写的,你替换一下吧,
正则部分为

/SELECT|select.*?FROM|from/
"SELECT id,other from t WHERE user_lock=1".replace(/SELECT|select.*?FROM|from/,"SELECT COUNT(*) FROM")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...