使用 PHP 和 preg_replace 拆分和替换内容而不是爆炸?

发布于 2024-10-12 07:03:09 字数 1572 浏览 3 评论 0原文

我有一个 PHP + HTML 代码作为包含函数的字符串。我希望用包含的内容替换一些函数调用。将多个文件合并为一个。

简短示例 - 之前

<html>
    <?php myclass->my_function('one', 'two', 'three'); ?>
    <h1>Content in between</h1>
    <?php myclass->my_function('1', '2'); ?>
</html>

简短示例 - 替换内容

<html>
    <?php run('one', 'two', 'three'); /* Run does include a file */ ?>
    <h1>Content in between</h1>
    <?php run('1', '2'); /* Run does include a file */ ?>
</html>`

简短示例 - 之后

<html>
    <?php
    /* Start my_function */
        echo 'This is the result of my_function one two three';
    /* End my_function
    ?>
    <h1>Content in between</h1>
    <?php
    /* Start my_function */
        <?php myclass->my_function('four', 'five', 'six'); ?>
    /* End my_function
    ?>
</html>

请注意,在包含的内容中找到了 myclass。结果需要再次解析。

简短示例 - 之后

再次解析整个内容,并用包含的内容替换 myclass。

<html>
    <?php
    /* Start my_function */
        echo 'This is the result of my_function one two three';
    /* End my_function */
    ?>
    <h1>Content in between</h1>
    <?php
    /* Start my_function */
        echo 'four five six';
    /* End my_function */
    ?>
</html>

我尝试用爆炸来做到这一点,但它变得复杂。 “替换内容”和“之后”这两个步骤可能需要一次性完成。

将其视为一个字符串。 preg_replace 可以解决这个问题吗?如何?

I have a PHP + HTML code as a string containing functions. I want some function calls to be replaced with included content. Put together many files to one.

Short example - Before

<html>
    <?php myclass->my_function('one', 'two', 'three'); ?>
    <h1>Content in between</h1>
    <?php myclass->my_function('1', '2'); ?>
</html>

Short example - Replacing content

<html>
    <?php run('one', 'two', 'three'); /* Run does include a file */ ?>
    <h1>Content in between</h1>
    <?php run('1', '2'); /* Run does include a file */ ?>
</html>`

Short example - After

<html>
    <?php
    /* Start my_function */
        echo 'This is the result of my_function one two three';
    /* End my_function
    ?>
    <h1>Content in between</h1>
    <?php
    /* Start my_function */
        <?php myclass->my_function('four', 'five', 'six'); ?>
    /* End my_function
    ?>
</html>

Notice that myclass is found in the included content. The result needs to be parsed again.

Short example - After that

The whole this is parsed again and it replaced the myclass with included content.

<html>
    <?php
    /* Start my_function */
        echo 'This is the result of my_function one two three';
    /* End my_function */
    ?>
    <h1>Content in between</h1>
    <?php
    /* Start my_function */
        echo 'four five six';
    /* End my_function */
    ?>
</html>

I tried to do this with explode but it went to complex. The two steps "Replacing content" and "After" might need to be done in one move.

Look at it as a string. Can preg_replace solve this? How?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想你的星星会说话 2024-10-19 07:03:09
$str = '<html>
<?php myclass->my_function(\'styles\', \'home.css\'); ?>
<p>Some other content</p>
<?php myclass->my_function(1, 2, 3); ?>
</html>';

function jens($matches)
{ 
    $path = '';
    $parts = explode(',', $matches[1]);
    foreach($parts as $match)
        $path .= '/' . str_replace('\'', '', trim($match));

    return $path;
}

$replaced = preg_replace_callback('/<\?php myclass->my_function\((.*?)\); \?>/', 'jens', $str);

echo $replaced;

应该做你想做的事。

$str = '<html>
<?php myclass->my_function(\'styles\', \'home.css\'); ?>
<p>Some other content</p>
<?php myclass->my_function(1, 2, 3); ?>
</html>';

function jens($matches)
{ 
    $path = '';
    $parts = explode(',', $matches[1]);
    foreach($parts as $match)
        $path .= '/' . str_replace('\'', '', trim($match));

    return $path;
}

$replaced = preg_replace_callback('/<\?php myclass->my_function\((.*?)\); \?>/', 'jens', $str);

echo $replaced;

Should do what you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文