php - 我想将字符串的某些部分从“此匹配”删除为“另一个匹配”
问题描述
我正在尝试使用该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()
我希望有人能理解我的问题:-)
解决方案
这有点要求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
推荐阅读
- javascript - 保存javascript函数
- r - 从数据集本身向 ggplot 添加标题
- java - 在类路径中找不到任何 META-INF/persistence.xml 文件,但我已经添加了它
- .net - 如何通过 login.microsoftonline.com 身份验证?
- vb.net - 从转换为字符串的 ListBox 中删除重复项
- webstorm - 有没有人有一个在虚拟环境中拥有 Node.js 然后在 WebStorm 中使用它的具体示例?
- javascript - 如何根据单元格值添加工作表?
- c - 如何在最后一次尝试中停止解析列表而不会不匹配?
- java - Java pword-generator 不会在循环内随机更新
- python - 使用 smtplib 发送熊猫数据框