I have written a PHP script that goes through and changes the HTML generated by Visual Page. (I know - it is a REALLY old program - but I like it.) Anyway, in each of these HTML web pages I'm working with I put in:
{copyright}
Where I want the copyright to show up. I did the following loop:
foreach( $file as $k1=>$v1 ){
if( preg_match("/\{copyright\}/i", $v1) ){
$file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 );
}
}
This DID NOT WORK. I would echo out the $file[$k1] before and after the IF statement so I could see what was going on and PHP just wouldn't change the {copyright} to the copyright. The variable $copyright had something similar to:
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
Now - here is the freaky thing: I put a BREAK after it did the preg_replace - and - it worked. So just changing the above to
foreach( $file as $k1=>$v1 ){
if( preg_match("/\{copyright\}/i", $v1) ){
$file[$k1] = preg_replace( "/\{copyright\}/i", $copyright, $v1 );
break;
}
}
Made it work. Does anyone have ANY kind of an idea why? I'm completely stumped by this.
Note: I thought I'd post what I had gotten the HTML down to.
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>
That is what I boiled my test case down to.
Also note : I did get this to work. Don't know why I had to put in a BREAK statement and I put it in on a whim. My thinking went "Maybe there is something that is making it re-evaluate the string after the change? Let me try putting in a break statement." I did and - it worked. But I have no idea WHY it worked.
Maybe I'm missing something here but it doesn't seem like you need any regex's here. The str_replace
function, http://php.net/str_replace, should work fine for this.
Example:
$string = "<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>";
$copyright = '<tr><td>Copyright 2007-NOW MyCompany. All 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 is a test</td></tr>
<tr><td>Copyright 2007-NOW MyCompany. All 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 is a test</td></tr>
{copyright}
</tbody></table>
</body>
</html>";
$copyright = '<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>';
foreach(explode("
", $string) as $line) {
echo str_replace('{copyright}', $copyright, $line) . "
";
}
Output:
<html>
<head>
<title>Test</title>
</head>
<body>
<table border='1' cellspacing='0' cellpadding='0'><tbody>
<tr><td>This is a test</td></tr>
<tr><td>Copyright 2007-NOW MyCompany. All rights reserved.</td></tr>
</tbody></table>
</body>
</html>
Although I am not yet sure exactly what is wrong - with the help of chris85 I no longer believe it is a PHP problem but instead is probably a hardware problem.
Chris posted to the PHP sandbox what I said I was using and it worked without a flaw. When I tried it ON the PHP sandbox (which means it used the sandbox's PHP interpreter) - it also worked without a problem.
Then I modified the program so it did the same thing but in two other ways. Thus, test #1 was just the single string, test #2 was the exploded string, and test #3 echoed out each individual string as it was modified.
On >MY< machine, the third method caused unexpected output to be generated. As stated though, the PHP sandbox computer did NOT have anything strange happen to it. Thus, this means that there is a hardware problem with my computer and not with PHP.
Sandbox link: http://sandbox.onlinephpfunctions.com/code/f34f57f6521cc6eebcc704b7c127974c0403834d
This question can now be closed.