<?php
$text = 'This is a {1} day, not {2} and {3}.';
$daytype = array( 1 => 'fine',
2 => 'overcast',
3 => 'rainy' );
while (ereg ('{([0-9]+)}', $text, $regs)) {
$found = $regs[1];
$text = ereg_replace("\{".$found."\}", $daytype[$found], $text);
}
echo "$text\n";
// This is a fine day, not overcast and rainy.
?>
$text = 'This is a {1} day, not {2} and {3}.';
$daytype = array( 1 => 'fine',
2 => 'overcast',
3 => 'rainy' );
while (ereg ('{([0-9]+)}', $text, $regs)) {
$found = $regs[1];
$text = ereg_replace("\{".$found."\}", $daytype[$found], $text);
}
echo "$text\n";
// This is a fine day, not overcast and rainy.
?>
结果:This is a fine day, not overcast and rainy
PHP字符串替换示例
本文通过一个简单的PHP脚本展示了如何使用正则表达式进行字符串中的变量替换。具体实现了将预定义的字符串模板中的占位符替换为实际的天气类型。

381

被折叠的 条评论
为什么被折叠?



