php - str_replace work incorrect ("str_replace" makes changes to the $replace parameter) -
php - str_replace work incorrect ("str_replace" makes changes to the $replace parameter) -
good day! specify, whatever reason, if number greater 10, str_replace() makes changes $replace parameter, cutting units , leaving dozens?
input info ($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> ... very simple php code:
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); } result $data incorrect. (if $axx > 10) why?
... <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> ... please help.
it because in first iteration 1's become [1]'s means 12 become [1]2 , never match agains 12 anymore.
instead of loops, utilize 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); result:
<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> php string str-replace
Comments
Post a Comment