[Løst] Substring - String Task

  

5
Emne starter

On the first line you will receive a string. On the second line you will receive a second string. Write a program that removes all of the occurrences of the first string in the second until there is no match. At the end print the remaining string.

Examples:

substring string task

3 Svar
4

My solution:

<?php
 
$replacement = readline();
$str = readline();
$count = 1;
 
while ($count > 0) {
    $strNew = str_replace($replacement, "", $str, $count);
    if ($str == $strNew) {
        break;
    } else {
        $str = $strNew;
    }
}
 
echo $strNew;
3

This is mine code:

<?php
 
$replacement = readline();
$str = readline();
 
while (true) {
    $strNew = str_replace($replacement, "", $str);
    if ($str == $strNew) {
        break;
    } else {
        $str = $strNew;
    }
}
 
echo $strNew;
1

This one is mine:

<?php
 
$replacement = readline();
$str = readline();
 
while (strpos($str, $replacement) !== false) {
    $str = str_replace($replacement, "", $str);
}
 
echo $str;
Del: