假设:
(30, 50, 55, 85, 120)
12022
,502
,55123
通缉:
22
,2
,123
12022
=> 22
,1202
=> 2
,502
=> 2
,55123
=> 123
,5050
=> 50
我可以想出各种方法来做到这一点,但寻找简洁的东西。奖金,如果它也美观:)
答案 0 :(得分:0)
我的尝试:
$v = array(120, 50, 55);
print removeModel("502", $v);
function removeModel($str, $v)
{
foreach($v as $value)
if (preg_match("/^$value/",$str))
return preg_replace("/$value/", "", $str);
}
答案 1 :(得分:0)
您可以使用substr($yourString, $x)
功能从$x
剪切$yourString
个字符。
我的想法:
<?php
$str = "502"; //or "12022" for testing
if(strlen($str)==3)
$newStr=substr($str, (strlen($str)-1)); //502 -> 2
else
$newStr=substr($str, (strlen($str)-2)); //12022 -> 22
echo ltrim($newStr, '0');
?>
答案 2 :(得分:0)
<?php
$arr = array(30, 50, 55, 85, 120); // Array of forbiddens
$str = "12022, 502, 55123"; // Your string
$sep = array_map('trim', explode(",", $str));
foreach($sep as $key => $value)
foreach ($arr as $sec)
if(preg_match("@^$sec@", $value))
$sep[$key] = substr($value, strlen($sec));
print_r($sep);
<强> 输出 强>:
Array
(
[0] => 22
[1] => 2
[2] => 123
)
答案 3 :(得分:-4)
只需循环数组并将字符串中的每个数组值替换为空字符串