SAQS or Super Awesome Query Stringing is a PHP class which allows you to easily use and manage query strings which are delminated by forward-slashes (/).
Once you have SAQS downloaded, add the following lines to the file you wish to use SAQS in.
require_once 'class_saqs.php';
//Init SAQS
if(!isset($saqs))
{
$saqs = new saqs;
}
The grave accent (`) can be used to enclose mutliple values containing forward-slashes. For example:
echo $saqs->set_qstr_value('cat', '`this/contains/slashes`');
Methods
The following methods are avaible through the SAQS class.
- $saqs->get_qstr();
- Returns the current query string as a un-urldecoded string.
- $saqs->get_qstr_value(string $variable);
- Returns the value of the $variable.
- $saqs->set_qstr_value(mixed $variable, mixed $value);
- Return the current query string modified with the $variable and $value passed
Example Script
<?php
error_reporting(E_ALL);
require_once 'class_saqs.php';
//Init SAQS
if(!isset($saqs))
{
$saqs = new saqs;
}
//Get the current query string.
echo 'Query string = '.$saqs->get_qstr()."<br><br>\n";
//Get a specific (cat) variable.
echo 'Variable <i>cat</i> = '.$saqs->get_qstr_value('cat')."<br>\n".
'Variable <i>color</i> = '.$saqs->get_qstr_value('color')."<br>\n".
'Variable <i>pill</i> = '.$saqs->get_qstr_value('pill')."<br>\n".
'Variable <i>material</i> = '.$saqs->get_qstr_value('material')."<br><br>\n";
//Add/Change a variable (cat) to the query string.
echo '<a href="'.$_SERVER['SCRIPT_NAME'].$saqs->set_qstr_value('cat', 'meow').'">Add/Change cat = meow</a><br>';
//Add/Change a variable (cat) in the query string.
echo '<a href="'.$_SERVER['SCRIPT_NAME'].$saqs->set_qstr_value('cat', 'woof').'">Add/Change cat = woof</a><br>';
//Add multiple variables (color, pill, and material) to the query string.
$variable_value_array = array('color' => 'blue', 'pill' => 'lithium', 'material' => 'plastic');
echo '<a href="'.$_SERVER['SCRIPT_NAME'].$saqs->set_qstr_value($variable_value_array).'">Add multiple variables</a><br><br>';
//Remove a variable (cat) from the query string.
echo '<a href="'.$_SERVER['SCRIPT_NAME'].$saqs->set_qstr_value('cat', '').'">Remove cat</a><br>';
//Remove multiple variables (color, pill, and material) from the query string.
$variable_value_array = array('color' => '', 'pill' => '', 'material' => '');
echo '<a href="'.$_SERVER['SCRIPT_NAME'].$saqs->set_qstr_value($variable_value_array).'">Remove multiple variables</a><br><br>';
//Using the grave accent and forward-slashes
echo '<a href="'.$_SERVER['SCRIPT_NAME'].$saqs->set_qstr_value('cat', '`this/contains/slashes`').'">Forward-Slashes` are safe in `/`</a><br><br>';
//Link to another site with this query string
echo '<a href="http://example.com/index.php'.$saqs->get_qstr().'" target="_blank">External Site</a><br>';
?>