Example #1
0
/**
 * Removes a tail from a string if it exists
 *
 * @param String $string The base string
 * @param String $tail The string to remove from the end of the base string, if it is there
 * @param Boolean $ignoreCase Whether the comparison should be case sensitive
 * @param String Returns the string without its tail
 */
function stripTail($string, $tail, $ignoreCase = TRUE)
{
    $string = (string) $string;
    $tail = (string) $tail;
    if (!\r8\str\endsWith($string, $tail, $ignoreCase)) {
        return $string;
    }
    return substr($string, 0, 0 - strlen($tail)) ?: "";
}
Example #2
0
 public function testEndsWith()
 {
     $this->assertTrue(\r8\str\endsWith('string with content', 'content'));
     $this->assertTrue(\r8\str\endsWith('string with content', 'Content'));
     $this->assertTrue(\r8\str\endsWith('string with content', 'content', TRUE));
     $this->assertTrue(\r8\str\endsWith('string with content', 'Content', TRUE));
     $this->assertTrue(\r8\str\endsWith('string with content', 'content', FALSE));
     $this->assertFalse(\r8\str\endsWith('string with content', 'Content', FALSE));
     $this->assertFalse(\r8\str\endsWith('string with content', 'contnt', FALSE));
 }
Example #3
0
 /**
  * Validates an e-mail address
  *
  * @param mixed $value The value to validate
  * @return String|NULL Any errors encountered
  */
 protected function process($value)
 {
     $value = (string) $value;
     if (\r8\isEmpty($value)) {
         return "Email Address must not be empty";
     }
     $atCount = substr_count($value, "@");
     if ($atCount == 0) {
         return "Email Address must contain an 'at' (@) symbol";
     }
     if ($atCount > 1) {
         return "Email Address must only contain one 'at' (@) symbol";
     }
     if (\r8\str\contains(" ", $value)) {
         return "Email Address must not contain spaces";
     }
     if (\r8\str\contains("\n", $value) || \r8\str\contains("\r", $value)) {
         return "Email Address must not contain line breaks";
     }
     if (\r8\str\contains("\t", $value)) {
         return "Email Address must not contain tabs";
     }
     if (preg_match('/\\.\\.+/', $value)) {
         return "Email Address must not contain repeated periods";
     }
     if (preg_match('/[^a-z0-9' . preg_quote('!#$%&\'*+-/=?^_`{|}~@.[]', '/') . ']/i', $value)) {
         return "Email Address contains invalid characters";
     }
     if (\r8\str\endsWith($value, ".")) {
         return "Email Address must not end with a period";
     }
     list($local, $domain) = explode("@", $value);
     if (\r8\str\startsWith($local, ".")) {
         return "Email Address must not start with a period";
     }
     // This is hard to describe to a user, so just give them a vague description
     if (\r8\str\endsWith($local, ".")) {
         return "Email Address is not valid";
     }
     if (strlen($local) > 64 || strlen($domain) > 255) {
         return "Email Address is too long";
     }
     $regex = '/' . '^' . '[\\w!#$%&\'*+\\/=?^`{|}~.-]+' . '@' . '(?:[a-z\\d][a-z\\d-]*(?:\\.[a-z\\d][a-z\\d-]*)?)+' . '\\.(?:[a-z][a-z\\d-]+)' . '$' . '/iD';
     // Do a final regex to match the basic form
     if (!preg_match($regex, $value)) {
         return "Email Address is not valid";
     }
 }