string - PHP find numbers in code and on second search skip first number? -
i'm working on little code, because 1 of colleges "destroyed" , xml document. want find y="\d+" in document , increase digits +3. attempt this:
$path_to_file = 'testing.xml'; $file_contents = file_get_contents($path_to_file); if(preg_match('/y="\d+"/', $file_contents, $bef)){ $length = count($bef); for($i=0; $i<$length; $i++){ if($bef[$i]!=0){ $file_contents = str_replace($bef[$i], 'y="'. $bef[$i]+3 .'"', $file_contents); } } file_put_contents($path_to_file,$file_contents); } else{ echo 'not found'; }
it seems find first number in document, can't figure out how search 2. , 3. etc. there resources can give me can find solution problem?
edit:
to show problem was:
i have xml code "< graphic-area y="125" x="324" width="bla" height="bla"> < /graphic-area>", etc. code later generated .pdf document. college didnt add trim site, every y property, except y="0" 3 mm low. , have 60+ pages lets 400+ areas y properties. needed php code y properties fixed. solution works fine, without '25.2' numbers, without numbers dot. solution use 2 different patterns , number before dot , increase it. here code best answer without "253.2232" solution.
$path_to_file = 'testing.xml'; $file_contents = file_get_contents($path_to_file); $pattern = '/y="(\d+)"/'; $new_contents = preg_replace_callback($pattern, 'add_three', $file_contents); file_put_contents($path_to_file,$new_contents); function add_three($matches){ return 'y="' . (3 + (int)$matches[1]) . '"'; }
hope helps someone
$path_to_file = 'testing.xml'; $file_contents = file_get_contents($path_to_file); $pattern = '/y="(\d+\.?\d*)"/'; // optional dot follower optional digits $new_contents = preg_replace_callback($pattern, 'add_three', $file_contents); file_put_contents($path_to_file,$new_contents); function add_three($matches){ return 'y="' . (3 + (float)$matches[1]) . '"'; // cast float not int, since can floats }
this call add_three
ever match in document.
Comments
Post a Comment