Skip to content

Instantly share code, notes, and snippets.

@wilforlan
Created February 16, 2018 00:13
Show Gist options
  • Save wilforlan/e65a17ad6b1dfbf6fd92ef76c1309b9b to your computer and use it in GitHub Desktop.
Save wilforlan/e65a17ad6b1dfbf6fd92ef76c1309b9b to your computer and use it in GitHub Desktop.
PHP function that determines if a string starts with an upper-case letter A-Z
<?php
/**
* This class contains sting helper functions.
*
* @package StringHelper
* @author Williams Isaac
* @version 1.0
*/
class StringHelper {
private $string_val = null;
private $on_position = null;
function __construct() {
}
/**
* Setter function to ser falues.
*
* @name caseChecker
* @var string $string String to check for
* @var string $position Position to check for, default 0
*/
public function caseChecker($string, $position = 0) {
if(!$this->testIsString($string)){
throw new Exception("This value: ".$string." is not a valid string");
}else{
$this->string_val = $string;
$this->on_position = $position;
return $this;
}
}
/**
* Test if the string is valid.
*
* @name testIsString
* @var string $string String to check for
*/
private function testIsString($string){
return is_string($string);
}
/**
* Test if string contains special characters
*
* @name containsSpecialChars
*/
public function containsSpecialChars(){
if(!ctype_alnum(str_replace(array('-', '_'), '', $this->string_val))){
throw new Exception("This value: ".$this->string_val." cannot contain special characters");
}else{
return $this;
}
}
public function val(){
return ctype_upper($this->string_val[$this->on_position]);
}
}
(new StringHelper)->caseChecker("Shh%d")->containsSpecialChars()->val(); //Exception: This value: Shh%d cannot contain special characters
(new StringHelper)->caseChecker("Shhd")->val(); // true
(new StringHelper)->caseChecker("shhd")->val(); // false
(new StringHelper)->caseChecker(4492)->containsSpecialChars()->val(); //Exception: This value: 4492 is not a valid string
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment