<?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正则表达式进行字符串中特定数值的查找与替换,通过实例解析并展示最终输出结果。
374

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



