How to remove characters from a string?
Posted
by masato-san
on Stack Overflow
See other posts from Stack Overflow
or by masato-san
Published on 2010-06-01T04:00:15Z
Indexed on
2010/06/01
4:03 UTC
Read the original article
Hit count: 240
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";
}
© Stack Overflow or respective owner