dongshu4221 2017-12-06 19:41
浏览 52

Str_Replace正在取代php语法

functions/SkriptParser.php

<?php
    $text = file_get_contents(basename($_SERVER['PHP_SELF']));
    preg_match_all("/{(.*?)}/", $text, $matches);
    var_dump($matches[0]);
    echo str_replace($matches[0],"Test",$text);
?>

Is my current code, this is called from my index page which is here:

index.php

require_once 'functions/SkriptParser.php';

When i open index.php it replace the correct strings [ It'll replace any string inside {} however, it seems to be replacing <?php and I have no clue why.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dongyao2022 2017-12-07 17:58
    关注

    I'm not sure I fully understand what you are trying to do.

    This line $text = file_get_contents(basename($_SERVER['PHP_SELF']));resolves to a local filename which will be the name of the script it's included in.

    It won't load the page which that script would output, it will load the file without executing the code. The contents of $text will be a string containing the pure php content.

    When I run your code from the command line and use "Here is a test of {your code}" as my string the output is:

    {your code}<?php
    require_once 'includetest.php';
    print_r(basename($_SERVER['PHP_SELF']));
    echo "Here's a test of Test";
    

    If I run it from a browser and view the source I see that too, but the browser escapes the php so it isn't rendered on the page. So (for me at least), your concept kinda works. However, the fact the code isn't executed means it will never do what you intend.

    Here's what I don't really understand though. In essence within index.php what you are telling your code to do is load a script which will load another copy of index.php and replace content within it.

    If you want to change the behaviour of index.php then alter the code within index.php, don't generate unsuitable output and then load another script in an attempt to parse it before returning it. Just output it the way you want the first time.

    Where is the content within the {} that you want to remove coming from? Why do you want/need to remove it? If you can clarify exactly what your aim is then it will be easier to suggest a solution.

    评论

报告相同问题?

悬赏问题

  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事: