我正在嘗試做的一個非常簡化的版本:$quote = "Currently showing number $i";for($i=0;$i<100;$i++){echo $quote;}其中$ifrom$quote不斷更新為新值。這又是一個簡化的例子。我意識到它可以重新排序以完成相同的事情或str_replace()使用過,但對于真正的代碼,它無法完成。
3 回答
斯蒂芬大帝
TA貢獻1827條經(jīng)驗 獲得超8個贊
這是你的答案
<?php
$quote = 'Currently showing number $i';
$pattern = '/\$([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)/';
preg_match_all($pattern, $quote, $matches);
for ($i=0; $i<100; $i++) {
foreach ($matches[1] as $index => $valName) {
if (isset(${$valName})) {
$result = str_replace($matches[0][$index], ${$valName}, $quote);
}
}
echo $result;
}
- 3 回答
- 0 關(guān)注
- 217 瀏覽
添加回答
舉報
0/150
提交
取消
