Skip to content
Advertisement

Remove Parts of String in php [closed]

is there any way to do this in PHP?

$first_string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$second_strong = "GHIJKLM";
function str_slayer($first_string, $second_string ){
  //doing some stuff
  return $data// return "ABCDEFNOPQRSTUVWXYZ";
}

it means I want a PHP function with two string parameters. 2nd parameter is a part of 1st parameter. the function will cut the 2nd string part from the 1st string and the rest of the 1st string will return.

thanks for your attention.

Advertisement

Answer

You can use the function str_replace() to replace “He” for “”. The code would look like this:

$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$substring = "GHIJKLM";
$final_string = str_replace($substring, "", $string);
echo $final_string;

The output would be:

ABCDEFNOPQRSTUVWXYZ

The str_replace() function will replace a substring, in your case “GHIJKLM”, for an other string, in this case we will use “” because we want to remove it from the $string.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement