douju7765 2015-07-05 16:18
浏览 17
已采纳

PHP脚本破坏了版权

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.

  • 写回答

2条回答 默认 最新

  • dongliyu3278 2015-07-05 17:29
    关注

    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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分