Regular Expressions 101

Community Patterns

Community Library Entry

1

Regular Expression
PCRE (PHP <7.3)

/
(sst=[^&]+&)|((\?|&|^)sst=[^&]+$)
/
gm

Description

This pattern finds a parameter (sst) key + value in a GET request URL. When replaced with an empty string, effectively removes the parameter. Works with both full URLs and isolated query strings. Works in PCRE and PCRE2.

Full use case:

if (isset($_GET["sst"])) {

   // validate

   // modify active session

   $query = $_SERVER["REQUEST_URI"];
   $pattern = "/(sst=[^&]+&)|((\?|&|^)sst=[^&]+$)/";
   $uri = preg_replace($pattern, "", $query);

   unset($_GET["sst"]);

   header("location: https://example.com$uri", true, 308);
}
Submitted by Martin Holt - a year ago (Last modified a year ago)