用不同的字符串替换相同的字符

时间:2022-11-24 11:47:43

Assuming I have a string

假设我有一个字符串

$str = "abc*efg*hij*";

and an array

和一个数组

$arr = array("123","456","789");

Now I want to replace the *s in $str with the elements in $arr according to the positions.The first * replaced with $arr[0],the second replaced with $arr[1] etc.I check the function str_replace,though it accepts arrays as parameters but I found it did not work.And I cannot just use

现在我想根据位置用$ arr中的元素替换$ str中的*。第一个*替换为$ arr [0],第二个替换为$ arr [1]等。我检查函数str_replace,虽然它接受数组作为参数,但我发现它不起作用。我不能只使用

$newstr = "abc{$arr[0]}efg{$arr[1]}hij{$arr[2]}"

because the real $str may be quite a long string with lots of *.Any good ideas?Thanks.

因为真正的$ str可能是一个很长的字符串,有很多*。很好的想法?谢谢。

2 个解决方案

#1


12  

If * is your only format character, try converting * to %s (also escape existing % to %%), and then using vsprintf(), which takes an array of values to pass in as format parameters:

如果*是您唯一的格式字符,请尝试将*转换为%s(也将现有%转义为%%),然后使用vsprintf(),它将一组值作为格式参数传入:

$str = str_replace(array('%', '*'), array('%%', '%s'), $str);
$newstr = vsprintf($str, $arr);
echo $newstr;

Output:

abc123efg456hij789

Note that if you have more array elements than asterisks, the extra elements at the end simply won't appear in the string. If you have more asterisks than array elements, vsprintf() will emit a too-few-arguments warning and return false.

请注意,如果您有比星号更多的数组元素,则末尾的额外元素将不会出现在字符串中。如果你有比星号元素更多的星号,vsprintf()将发出太少的参数警告并返回false。

#2


1  

You could always just keep it simple with preg_replace() and make use of the $limit argument, like so:

您可以随时使用preg_replace()保持简单并使用$ limit参数,如下所示:

for($i = 0; $i < count($arr); $i++)
    $str = preg_replace('/\*/', $arr[$i], $str, 1);

but for practicality's sake, @BoltClock's answer is the better choice as it a) does not involve a loop, but more importantly b) is not forced to use a regular expression.

但为了实用,@ BoltClock的答案是更好的选择,因为它a)不涉及循环,但更重要的是b)不被强制使用正则表达式。

#1


12  

If * is your only format character, try converting * to %s (also escape existing % to %%), and then using vsprintf(), which takes an array of values to pass in as format parameters:

如果*是您唯一的格式字符,请尝试将*转换为%s(也将现有%转义为%%),然后使用vsprintf(),它将一组值作为格式参数传入:

$str = str_replace(array('%', '*'), array('%%', '%s'), $str);
$newstr = vsprintf($str, $arr);
echo $newstr;

Output:

abc123efg456hij789

Note that if you have more array elements than asterisks, the extra elements at the end simply won't appear in the string. If you have more asterisks than array elements, vsprintf() will emit a too-few-arguments warning and return false.

请注意,如果您有比星号更多的数组元素,则末尾的额外元素将不会出现在字符串中。如果你有比星号元素更多的星号,vsprintf()将发出太少的参数警告并返回false。

#2


1  

You could always just keep it simple with preg_replace() and make use of the $limit argument, like so:

您可以随时使用preg_replace()保持简单并使用$ limit参数,如下所示:

for($i = 0; $i < count($arr); $i++)
    $str = preg_replace('/\*/', $arr[$i], $str, 1);

but for practicality's sake, @BoltClock's answer is the better choice as it a) does not involve a loop, but more importantly b) is not forced to use a regular expression.

但为了实用,@ BoltClock的答案是更好的选择,因为它a)不涉及循环,但更重要的是b)不被强制使用正则表达式。