首页 > 解决方案 > 我想将字符串的某些部分从“此匹配”删除为“另一个匹配”

问题描述

我正在尝试使用该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()

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

标签: phpstringreplacetrim

解决方案


这有点要求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

推荐阅读