settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Substring - String Task

+7 votes

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

asked in PHP category by user ak47seo

3 Answers

+6 votes

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;

 

answered by user matthew44
+6 votes

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;
answered by user john7
+5 votes

This one is mine:

<?php

$replacement = readline();
$str = readline();

while (strpos($str, $replacement) !== false) {
    $str = str_replace($replacement, "", $str);
}

echo $str;
answered by user Jolie Ann
...