查找子字符串并将其替换为指定的字符 [英] Find a substring and replace it to a specified char

查看:60
本文介绍了查找子字符串并将其替换为指定的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个字符串,如下所示:

I got a string, built up like the following:

$string = 'HUB_NENNWERT_KATALOG:[0 TO 4.9],GREIFKRAFT_OEFFNEN_KATALOG:[2000 TO 5999],WERKSTUECKGEWICHT:[0 TO 14.9]';

此字符串中的整数可以不同.所以我想要的是,检查某个字段是否在字符串中,即HUB_NENNWERT_KATALOG".如果返回 true,我想删除包括逗号在内的整个子字符串.所以它会返回一个像这样的新字符串:

The ints in this String can be different. So what I want is, check if a certain field is in the String, i.e. 'HUB_NENNWERT_KATALOG'. If returned true, I want to delete the whole substring inclusive the comma. So it would return a new String like this:

$string = 'GREIFKRAFT_OEFFNEN_KATALOG:[2000 TO 5999],WERKSTUECKGEWICHT:[0 TO 14.9]';

我知道可能发生的所有字段,但不知道值.我如何实现这一目标?

I know alle fields, that can occur, but not the values. How do I achieve this?

希望描述得足够清楚.

推荐答案

你可以试试:

if(preg_match("/HUB_NENNWERT_KATALOG:\[.*\]/isU",$string){
    preg_replace("/HUB_NENNWERT_KATALOG:\[.*\]/isU","", $string);
}

这篇关于查找子字符串并将其替换为指定的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆