0

我不知道具体怎么说,我举个例子吧。

//[$var:Username|n] will print username with n length
$myText = 'The participants are [$var:Username|10], [$var:Username|8], and [$var:Username|6]';
$username = array('Alexandrite', 'Maximillian', 'Angelina');
$result = someFunction('[$var:Username|n]', $username, $myText);
echo $result;
//The participants are Alexandrit, Maximill, and Angeli

知道怎么做吗?也许使用 preg_replace?

4

1 回答 1

3

我很想知道为什么你甚至需要这样做。不管:

preg_replace_callback('/\[\$var:Username\|(\d+)\]/', function ($match)
    use (&$count, $username
) {
    return substr($username[$count++], 0, $match[1]);
}, $myText);
于 2013-02-16T03:16:47.353 回答