我想从“此匹配”到“另一个匹配”中删除字符串的一部分

时间:2019-09-06 11:48:55

标签: php string replace trim

我正在尝试使用trim()函数,但该函数无法正常工作。

我有一个像这样的字符串:

"Hello I have this code: This is important code here"

我想使用匹配的字符串值删除此字符串的某些部分。

示例:

我想在'Hello'上开始比赛

然后在'code: '

停止比赛

它应该输出:This is important code here

请帮助我解决问题,谢谢。

代码示例----->

$string = 'Hello friend, I have problem with some of string operation case; Please help me if you can';
$first_match = 'I have' ;
$second_match = 'case;' ;
$new_string = remove_function($first_match, $second_match, $string);

现在$new_string = 'Hello friend, Please help me if you can'; 我需要这种类型的输出,并寻找那种remove_function()

我希望有人会理解我的问题:-)

1 个答案:

答案 0 :(得分:1)

要问trim()

假设您的字符串中总是有一个冒号:,而您只想保留该冒号右边的内容,您可以这样操作

$str = "Hello I have this code: This is important code here";

if ( ($pos = strpos($str, ':')) !== FALSE ) {
    $new_str = substr($str, $pos+2);
}
echo $new_str;

结果:

This is important code here
相关问题