美好的一天!
无论出于何种原因,如果数字大于10,请指定str_replace()
更改$replace
参数,裁减单位并只留下几十个?
输入数据($data
):
...
<div onclick="window.location.href='/template-04.php?type=users&char=7';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=8';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=9';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=10';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=11';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=12';"></div>
...
非常简单的PHP
代码:
for($axx = 0; $axx < 68; $axx ++)
{
$z = '['.$axx.']';
$newName = 'templ4-user-'.$z.'.html?'.$z;
echo '<br>'.$newName; // echo (axx = 13): <br>templ4-user-[13].html?[13]
$data = str_replace('template-04.php?type=users&char='.$axx, $newName, $data);
}
结果$data
不正确。 (if $axx > 10
)为什么?
...
<div onclick="window.location.href='/templ4-user-[7].html?[7]';"></div>
<div onclick="window.location.href='/templ4-user-[8].html?[8]';"></div>
<div onclick="window.location.href='/templ4-user-[9].html?[9]';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]0';"></div> <------ !!!!!!!
<div onclick="window.location.href='/templ4-user-[1].html?[1]1';"></div> <------ !!!!!!!
<div onclick="window.location.href='/templ4-user-[1].html?[1]2';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]3';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]4';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]5';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]6';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]7';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]8';"></div>
<div onclick="window.location.href='/templ4-user-[1].html?[1]9';"></div>
<div onclick="window.location.href='/templ4-user-[2].html?[2]0';"></div>
...
请帮忙。
答案 0 :(得分:2)
这是因为在第一次迭代中,所有1将变为[1],这意味着12
将变为[1]2
并且将永远不再匹配12。
您可以使用preg_replace:
代替循环$data = <<<EOS
<div onclick="window.location.href='/template-04.php?type=users&char=7';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=8';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=9';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=10';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=11';"></div>
<div onclick="window.location.href='/template-04.php?type=users&char=12';"></div>
EOS;
$pattern = '/template-04.php\?type=users&char=(\d+)/i';
$replacement = 'templ4-user-[$1].html?[$1]';
echo preg_replace($pattern, $replacement, $data);
结果:
<div onclick="window.location.href='/templ4-user-[7].html?[7]';"></div>
<div onclick="window.location.href='/templ4-user-[8].html?[8]';"></div>
<div onclick="window.location.href='/templ4-user-[9].html?[9]';"></div>
<div onclick="window.location.href='/templ4-user-[10].html?[10]';"></div>
<div onclick="window.location.href='/templ4-user-[11].html?[11]';"></div>
<div onclick="window.location.href='/templ4-user-[12].html?[12]';"></div>