我有这个字符串:
525,294,475,215,365,745
我需要删除475 ..和逗号。 如果我需要删除第一个数字我还需要删除下一个逗号,也是最后一个。 我可以吗? 一个正则表达式? THX
答案 0 :(得分:2)
$newStr = str_replace(',475', '525,294,475,215,365,745');
或者错误较少的方式:
$new = array();
$pieces = explode(',', '525,294,475,215,365,745');
foreach ($pieces as $piece) {
if ($piece != 475) {
$new[] = $piece;
}
}
$newStr = implode(',', $new);
答案 1 :(得分:1)
这是一个正则表达式:
$s = "525,294,475,215,365,745";
$s = preg_replace(',?475', '', $s);
答案 2 :(得分:0)
$data = "525,294,475,215,365,745";
$parts = explode(',', $data);
for ($i = 0; $i < count($parts); $i++) {
if ($parts[$i] == 475) {
unset($parts[$i]);
}
}
$newdata = join(',', $parts);
答案 3 :(得分:0)
<?php
$bigString = "525,294,475,215,365,745";
$pos = strpos($bigString, ",");
while($pos != false) {
$newString .= substr($bigString, 0, $pos);
$bigString = substr($bigString, $pos + 1);
$pos = strpos($bigString, ",");
}
echo $newString;
?>
答案 4 :(得分:0)
function filterValue($index, &$a)
{
$key = array_search($index, $a);
if ($key != false) {
unset($a[$key]);
}
}
// Original data
$data = "525,294,475,215,365,745";
$data = explode(',', $data);
filterValue('475', $data);
$output = implode(',', $data);