<?php
// callback func is passed a single character and should return true for
// characters that are to be kept in the returned string, or false for
// characters that should be dropped.
function str_filter($s,$func){
$r = '';
for ($i = 0; $i < strlen($s); $i++){
$l = substr($s,$i,1);
if (call_user_func($func,$l)) $r .= $l;
}
return $r;
}
?>
str_filter() - array_filter() for strings
There may well come a time when it's useful to filter a string through a callback, dropping the characters that the callback dislikes and keeping/returning the rest. I use it here on defproc.co.uk when generating locators (the URLs for individual items).
(1) | by defproc on November 9th, 2007
An account is required to post comments and adaptations
COMMENTS AND ADAPTATIONS

I've added a size variable setting the size of the blocks passed to the function.
(1) | an adaptation by Jove on November 9th, 2007 | read entry | 0 comments | 1 adaptations
loading
