- str_starts_with: documentation ( source)
<?php
class DoctorFilter extends FilterIterator
{
public function accept(): bool
{
$obj = $this->getInnerIterator()->current();
return is_string($obj) && str_starts_with($obj, 'Dr.');
}
}
$people = [
'Dr. Jekyll',
'Mr. Hyde', // This item will be removed
'Dr. Frankenstein',
];
foreach (new DoctorFilter(new ArrayIterator($people)) as $item) {
echo $item.PHP_EOL;
}