Ayrıca bir sınıf veri çevreliyor olabilir, Menzil ki:
class Range {
protected $min;
protected $max;
public function __construct($str) {
if(preg_match('/^\d+$/', $str)) {
$this->min = (int)$str;
$this->max = (int)$str;
} else {
preg_match('/^(\d*)-(\d*)$/', $str, $matches);
$this->min = $matches[1] ? (int)$matches[1] : null;
$this->max = $matches[2] ? (int)$matches[2] : null;
}
}
// more functions here like contains($value) and/or min() and max()
public function __toString() {
return 'min=' . $this->min . ', max=' . $this->max;
}
}
$tests = array('40', '-40', '40-', '40-60');
foreach($tests as $t) {
echo new Range($t) . "\n";
}
üretir:
min=40, max=40
min=, max=40
min=40, max=
min=40, max=60
Tabii ki, preg_
bazı "normal" string fonksiyonları ile çağrıları yerini alabilir, ama ben PHP biliyorum tek şey bazı regex kandırmaca olduğunu.