<?php
function str_filter($s, $func, $size=1, $negated=false){
$r = '';
for ($i = 0; $i < (strlen($s)); $i+=$size){
$l = substr($s,$i,$size);
if (call_user_func($func,$l) xor $negated) $r .= $l;
}
return $r;
}
/* Usage:
function is_vowel($char){
return strpos('aeiou',strtolower($char)) !== false;
}
print str_filter("I am a mole and I live in a hole","is_vowel",1);
// only vowels: IaaoeaIieiaoe
print str_filter("I am a mole and I live in a hole","is_vowel",1,true);
// drop vowels: m ml nd lv n hl
*/
?>
str_filter() - with $size and $negated
On top of Jove's $size option I've added a switch to reverse the role of the callback: with $negated as true, characters (or chunks) that have the callback return false are kept and true discarded.
(1) | by defproc on November 16th, 2007 | adapts str_filter() - array_filter() for strings - adapted by Jove
loading
