我有一个这样的字符串:
$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);
请帮我解决这个问题。
答案 0 :(得分:2)
首先,我将尝试选择需要替换的字符串:
substr($str, strpos($str, ","), 3);
// Gives me ,39 - 3 characters from the ,.
现在我将其替换为.--
<?php
$str = "This product price is 23,39 and sale end after 29th June";
echo str_replace(substr($str, strpos($str, ","), 3), ".--", $str);
?>
这给了我一个输出:
This product price is 23.-- and sale end after 29th June
这是你需要的吗?这很简单,不需要RegEx。在这种情况下,不确定RegEx是否会更有效。我会这样做。