PHP删除匹配字符后替换3个字符

PHP删除匹配字符后替换3个字符,php,Php,我有这样一个字符串: $str = "This product price is 23,39 and sale end after 29th June"; 我需要将“,”后面的两个字符替换为“-”,因此输出如下: This product price is 23.- and sale end after 29th June 我尝试了,但删除了最后3个字符: echo substr($string, 0, strlen($string) - 3); 请帮助我解决此问题。首先,我将尝试选择需

我有这样一个字符串:

$str = "This product price is 23,39 and sale end after 29th June";
我需要将
“,”
后面的两个字符替换为
“-”
,因此输出如下:

This product price is 23.- and sale end after 29th June
我尝试了,但删除了最后3个字符:

 echo substr($string, 0, strlen($string) - 3);

请帮助我解决此问题。

首先,我将尝试选择需要替换的字符串:

substr($str, strpos($str, ","), 3);
// Gives me ,39 - 3 characters from the ,.
现在我将用
替换它--


这是你需要的吗?这很简单,不需要正则表达式。不确定在这种情况下正则表达式是否更有效。我将这样做。

首先,我将尝试选择需要替换的字符串:

substr($str, strpos($str, ","), 3);
// Gives me ,39 - 3 characters from the ,.
现在我将用
替换它--


这是你需要的吗?这很简单,不需要正则表达式。不确定在这种情况下正则表达式是否更有效。我会这样做。

正则表达式比
substr
更容易。使用
substr
时,您需要找到第一个实例,并用它代替
0
。e、 g.
strop
$string
不是
$str
不清楚您在代码中做什么。第二行代码的地址与第一行不同。此外,您似乎不需要搜索
。正则表达式比
substr
更容易。使用
substr
时,您需要找到第一个实例,并用它代替
0
。e、 g.
strop
$string
不是
$str
不清楚您在代码中做什么。第二行代码的地址与第一行不同。此外,您似乎没有搜索
@user3783243ha-Ha。。。没错。我刚刚添加了更多信息。@user3783243耶@用户3783243哈哈哈。。。没错。我刚刚添加了更多信息。@user3783243耶!
This product price is 23.-- and sale end after 29th June