str_replace函数;用* Minus The First替换所有字母?

时间:2013-01-11 03:16:52

标签: php

我试图用星号替换给定字符串的所有字母,减去第一个字母。我试过这个:

$AnswerArr = str_split($Answer);
$AnswerCount = count($Answer);
$Toreplace = $AnswerCount - 1;
$ReplaceAnswer = str_replace($AswerArr['0'], "*", $Answer, $Toreplace);

但这不起作用,我是否必须使用正则表达式?

使用提供的第一个答案:

Warning: substr() expects parameter 1 to be string, array given in /var/www/New/API/FormValidation.php on line 15

Warning: strlen() expects parameter 1 to be string, array given in /var/www/New/API/FormValidation.php on line 15

Warning: str_repeat() [function.str-repeat]: Second argument has to be greater than or equal to 0 in /var/www/New/API/FormValidation.php on line 15

2 个答案:

答案 0 :(得分:1)

怎么样:

$ReplaceAnswer = substr($Answer, 0, 1) . str_repeat('*', strlen($Answer) - 1);

答案 1 :(得分:1)

这应该有用。

$tmp = preg_replace('/(?!^)[\S \t]/', '*', $inputstr);