I was studying string manipulation.
You can use strlen() or substr() but cannot rely on other functions that are predefined in libraries.
Given string $string = "This is a pen", remove "is" so that
return value is "Th a pen" (including 3 whitespaces).
Remove 'is' means if a string is "Tsih", we don't remove it. Only "is" is removed.
I've tried (shown below) but the returned value is not correct. I've run test test and I'm still capturing the delimiter.
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";
}
strlen()a predefined function? If you can usestrlen()then you ought to be able to usesubstr()too...str_replace()is just as much a part of PHP's standard library as isstrlen(). You just use$string = str_replace('is', '', $string);. The hardest part is remembering which order the arguments go in and whether there's an underscore in the function name (PHP!!!) Also, in the expected output, is there supposed to be only one space in between "Th" and "a"? Or are there supposed to be two? If there's supposed to be only 1 then the problem is slightly more complicated, but only slightly.