Ferreteria/v0.6/clade/Sys/Data/Things/Array/types/AStr
Jump to navigation
Jump to search
| ||||||||||||||
Code
as of 2025-10-09
interface iAStr {
// SEARCH
function StartsWith(string $sAsk) : ?string;
}
trait tAStr { // IMPLEMENTS iAStr
// ++ CONFIG ++ //
protected function QVarClass(int|string $snKey) : string { return QStrClass::class; }
// -- CONFIG -- //
// ++ SEARCH ++ //
/**
* ACTION: find the best match for the given Ask string; return NULL if no match
* NOTE: for now, I'm allowing *either* A-begins-B *or* B-begins-A. That should probably be settable by flags.
*/
public function StartsWith(string $sAsk) : ?string {
$nMatch = 0;
$sFnd = NULL;
$nlAsk = strlen($sAsk);
foreach ($this as $sKey => $sVal) {
if ($sKey === $sAsk) {
// identical key found
$sFnd = $sKey;
} elseif (str_starts_with($sKey,$sAsk)) {
// does this key-string start with the Ask string?
if ($nlAsk > $nMatch) {
$sFnd = $sKey;
$nMatch = $nlAsk;
#echo CRLF.' -- BEST';
}
} elseif (str_starts_with($sAsk,$sKey)) {
// does the Ask string start with this key-string?
$nlKey = strlen($sKey);
if ($nlKey > $nMatch) {
$sFnd = $sKey;
$nMatch = $nlKey;
}
}
}
return $sFnd;
}
// -- SEARCH -- //
}