How to remove characters from a string?
- by masato-san
Hi,
Below is interview question so you cannot relay on the functions that predefined in libraries. Also my answer below set the element to null but there is another ways to
solve the problem.
Given string $string = "This is a pen", remove "is" so that
return value is "Th a pen" (including whitespece).
I've tried (shown below) but returned value is not correct.
Thanks in advance!
function remove_delimiter_from_string(&$string, $del) {
for($i=0; $i<strlen($string); $i++) {
for($j=0; $j<strlen($del); $j++) {
if($string[$i] == $del[$j]) {
$string[$i] = $string[$i+$j]; //this grabs delimiter :(
}
}
}
echo $string . "\n";
}