从数据中删除所有$ _POST和$ _GET

时间:2014-09-04 01:14:48

标签: php regex

我正在寻找一个preg_replace方法来从字符串中去除所有$_GET['whatever']和所有$_POST['values']

例如,如果字符串是:

$str = "this is some text with $_POST['foo'] within it - and some $_GET['bar'] as well";

我想让它回归:

$str = "this is some text with within it - and some as well";

感谢。

1 个答案:

答案 0 :(得分:2)

您编写的内容甚至无法运行,因为字符串中的引号会导致语法错误。你需要逃避他们,例如

$str = 'this is some text with $_POST[\'foo\'] within it - and some $_GET[\'bar\'] as well';

假设您已完成此操作,则可以使用以下命令删除这些字符串:

$str = preg_replace('/\$_(GET|POST)\[[^]]+\]/', '', $str);

只要您没有嵌套在括号内的任何数组引用,这将有效。