Sanitization
The packages/base/utility/safe class is used to sanitize the contents of strings, numbers, and dates. For ease of use, the methods of this class are written as static.
If you use the validator to check input data, the validator automatically performs all of these tasks.
For more information, refer to the Validation page.
Sanitizing Strings
Strings received from the user must have their content sanitized before any use. Strings received from the user may contain malicious code that disrupts the program. Using the string method, you can make the contents of a string safe to use. This method takes a string as its parameter, sanitizes its content through certain processing, and returns it as output.
safe::string(string);
Sanitizing Numbers
Determining the range and converting a numeric string into a number are among the tasks performed by the number method. This method takes a string or a number as its parameter. With the second parameter set to true or false, you can specify whether the number is allowed to be negative. After checking the string or the number's range, this method returns its numeric value as output.
safe::number(num, negative);
Sanitizing Dates
Using the date or is_date method, you can verify the validity of a string as a date. This method takes a string as its first parameter and, after sanitization, returns an array containing the year, month, and day fields.
safe::date(string);
Sanitizing Email Addresses
The is_email method validates an Email address against the format of an email address. The output of this method is true if the email address is valid and false otherwise.
safe::is_email(string);
Sanitizing Mobile Numbers
Using the is_cellphone_ir or cellphone_ir method, you can make sure that a number is a valid mobile number. As the method names indicate, this sanitization applies only to Iranian mobile numbers.
The cellphone_ir method sanitizes the mobile number based only on its length. If the sanitization is confirmed, then for the sake of standardization the output of this method is that same mobile number with Iran's country code (98) prepended to it; otherwise, it is false.
However, the is_cellphone_ir method also sanitizes the mobile number based on its content and the prefixes of Iranian operator companies. The output of this method is true if the mobile number's validity is confirmed and false otherwise.
safe::is_cellphone_ir(string);
safe::cellphone_ir(string);
Sanitizing IP Addresses
The is_ip4 method can sanitize an IP in terms of format and length. The output of this method is true if the sanitization is confirmed and false otherwise.
safe::is_ip4(string);
Example
$string= safe::string("jalno supported by 'jeyserver'");
echo $string;
/* output
jalno supported by jeyserver
*/
$date = "1397/02/03 12:00";
if ($result = safe::is_date($date)) {
print_r($result);
} else {
echo "Invalid date.";
}
/* output
Array
(
[Y] => 1397
[m] => 02
[d] => 03
[h] => 12
[i] => 00
)
*/
$cellphone = "09123456789";
if (safe::is_cellphone_ir($cellphone)) {
echo safe::cellphone_ir($cellphone);
} else {
echo "Invalid cellphone.";
}
/* output
989123456789
*/
$ip = "127.0.0.1";
var_dump(safe::is_ip4($ip));
/* output
bool(true)
*/