2014-09-04 124 views
-1

我正在尋找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

很酷,到目前爲止你有什麼? – danronmoon 2014-09-04 01:17:03

+0

是不是可以刪除$ str中的$ _POST和$ _GET變量? – kimbarcelona 2014-09-04 01:20:10

+2

如果是關於無效語法,如此處所示,而不是實際的字符串內容,那麼請使用您的編輯器搜索和替換函數。 – mario 2014-09-04 01:21:54

回答

2

你寫的assingnment將不會運行,因爲字符串內的引號會導致語法錯誤。你需要逃避它們,例如

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

假如你這樣做,你可以刪除這些字符串有:

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

這將工作,只要你沒有嵌套在括號內的任何數組引用。

+0

不需要轉義字符類中的方括號嗎? – Phil 2014-09-04 01:30:36

+0

如果這是班上的第一件事,則不行。 – Barmar 2014-09-04 01:31:07

+0

哦,很酷。不知道那一個 – Phil 2014-09-04 01:33:03