PHP script mucks up copyright -
i have written php script goes through , changes html generated visual page. (i know - old program - it.) anyway, in each of these html web pages i'm working put in:
{copyright}
where want copyright show up. did following loop:
foreach( $file $k1=>$v1 ){ if( preg_match("/\{copyright\}/i", $v1) ){ $file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 ); } }
this did not work. echo out $file[$k1] before , after if statement see going on , php wouldn't change {copyright} copyright. variable $copyright had similar to:
<tr><td>copyright 2007-now mycompany. rights reserved.</td></tr>
now - here freaky thing: put break after did preg_replace - , - worked. changing above to
foreach( $file $k1=>$v1 ){ if( preg_match("/\{copyright\}/i", $v1) ){ $file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 ); break; } }
made work. have kind of idea why? i'm stumped this.
note: thought i'd post had gotten html down to.
<html> <head> <title>test</title> </head> <body> <table border='1' cellspacing='0' cellpadding='0'><tbody> <tr><td>this test</td></tr> {copyright} </tbody></table> </body> </html>
that boiled test case down to.
also note : did work. don't know why had put in break statement , put in on whim. thinking went "maybe there making re-evaluate string after change? let me try putting in break statement." did , - worked. have no idea why worked.
maybe i'm missing here doesn't seem need regex's here. str_replace
function, http://php.net/str_replace, should work fine this.
example:
$string = "<html> <head> <title>test</title> </head> <body> <table border='1' cellspacing='0' cellpadding='0'><tbody> <tr><td>this test</td></tr> {copyright} </tbody></table> </body> </html>"; $copyright = '<tr><td>copyright 2007-now mycompany. rights reserved.</td></tr>'; echo str_replace('{copyright}', $copyright, $string);
output:
<html> <head> <title>test</title> </head> <body> <table border='1' cellspacing='0' cellpadding='0'><tbody> <tr><td>this test</td></tr> <tr><td>copyright 2007-now mycompany. rights reserved.</td></tr> </tbody></table> </body> </html>
demo: http://sandbox.onlinephpfunctions.com/code/f53b1a96f270e52392303d7dfb7c327372747d0b
update per comment:
$string = "<html> <head> <title>test</title> </head> <body> <table border='1' cellspacing='0' cellpadding='0'><tbody> <tr><td>this test</td></tr> {copyright} </tbody></table> </body> </html>"; $copyright = '<tr><td>copyright 2007-now mycompany. rights reserved.</td></tr>'; foreach(explode("\n", $string) $line) { echo str_replace('{copyright}', $copyright, $line) . "\n"; }
output:
<html> <head> <title>test</title> </head> <body> <table border='1' cellspacing='0' cellpadding='0'><tbody> <tr><td>this test</td></tr> <tr><td>copyright 2007-now mycompany. rights reserved.</td></tr> </tbody></table> </body> </html>
Comments
Post a Comment