如何用php填充逗号分隔的字符串?

I have a variable with the following value:
1,2,11,17,2
I need to convert it to:
SER1, SER2, SER11, SER17, SER2
In other words I need to pad to the left of each number the words SER
How to accomplish this with PHP?

You could use a regular expression to replace each number with SER prepended to the number: preg_replace('/(\d+)/', 'SER$1', '1,2,11,17,2');

You can use array_map to iterate through the array and prepend the string.

Example:

$array = array(1,2,11,17,2);

$new_array = array_map(function($value) {
    return 'STR' . $value;
}, $array);

var_dump($new_array);

Edit: Disregard, I thought you were working with an array.

Use concatenate for the first 'SER' and then use the replace function.

$StringVar="SER".$StringVar
$StringVar=str_replace(" ,",", SER",$StringVar) 

In the example of array_walk, you see this code:

<?php
$fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");

function test_alter(&$item1, $key, $prefix)
{
    $item1 = "$prefix: $item1";
}

function test_print($item2, $key)
{
    echo "$key. $item2<br />
";
}

echo "Before ...:
";
array_walk($fruits, 'test_print');

array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:
";

array_walk($fruits, 'test_print');
?> 

The above example will output:

Before ...:
d. lemon
a. orange
b. banana
c. apple
... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple