这个 preg_replace_callback 在 PHP 中做了什么? 以及如何阻止它泄漏内存?

发布于 2024-07-08 15:52:44 字数 429 浏览 5 评论 0原文

我在 b2evo PHP 网站上有一段代码,它执行以下操作:

$content = preg_replace_callback(
    '/[\x80-\xff]/',
    create_function( '$j', 'return "&#".ord($j[0]).";";' ),
    $content);

这部分代码的作用是什么? 我的猜测是它会删除 128 到 256 之间的 ASCII 字符,但我不能确定。

而且,就目前情况而言,每次从页面内调用这段代码时,PHP 都会分配最多 2K 的内存,然后不会释放。 如果该函数在一个页面上被调用 1000 多次(这种情况可能会发生),则该页面会额外使用 2MB 内存。

这导致我的网络应用程序出现问题。 为什么我会丢失内存,如何重写它以避免内存泄漏?

I've got a section of code on a b2evo PHP site that does the following:

$content = preg_replace_callback(
    '/[\x80-\xff]/',
    create_function( '$j', 'return "&#".ord($j[0]).";";' ),
    $content);

What does this section of code do? My guess is that it strips out ascii characters between 128 and 256, but I can't be sure.

Also, as it stands, every time this bit of code is called from within a page, PHP allocates and then does not free upto 2K of memory. If the function is called 1000+ times on a page (this can happen), then the page uses an extra 2MB of memory.

This is causing problems with my web application. Why am I losing memory, and how do I rewrite this so I don't get a memory leak?

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

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

发布评论

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

评论(3

多情癖 2024-07-15 15:52:44

create_function 正在泄漏您的内存 - 只需使用普通函数即可。

该函数本身用数字 HTML 实体替换字符 (&#xxx;)

It's create_function that's leaking your memory - just use a normal function instead and you'll be fine.

The function itself is replacing the characters with numeric HTML entities (&#xxx;)

魔法唧唧 2024-07-15 15:52:44

并不是真正的剥离,它用实体替换了高 Ascii 字符。

请参阅 preg_replace_callback.
create_function 用于创建匿名函数,但您可以使用普通函数代替:

$content = 'Çà ! Nœm dé fîçhïèr tôrdù, @ pöür têstër... ? ~ Œ[€]';
$content = preg_replace_callback('/[\x80-\xff]/', 'CB_CharToEntity', $content);
echo $econtent . '<br>';
echo htmlspecialchars($content) . '<br>';
echo htmlentities($content) . '<br>';
echo htmlentities($content, ENT_NOQUOTES, 'cp1252') . '<br>';

function CB_CharToEntity($matches)
{
    return '&#' . ord($matches[0]) . ';';
}

[编辑] 找到了一种更干净、可能更快的方法来完成这项工作! ^_^ 只需使用 htmlentities 以及适合您需求的选项即可。

Not really stripping, it replaces high-Ascii characters by their entities.

See preg_replace_callback.
create_function is used to make an anonymous function, but you can use a plain function instead:

$content = 'Çà ! Nœm dé fîçhïèr tôrdù, @ pöür têstër... ? ~ Œ[€]';
$content = preg_replace_callback('/[\x80-\xff]/', 'CB_CharToEntity', $content);
echo $econtent . '<br>';
echo htmlspecialchars($content) . '<br>';
echo htmlentities($content) . '<br>';
echo htmlentities($content, ENT_NOQUOTES, 'cp1252') . '<br>';

function CB_CharToEntity($matches)
{
    return '&#' . ord($matches[0]) . ';';
}

[EDIT] Found a cleaner, probably faster way to do the job! ^_^ Just use htmlentities with options fitting your needs.

天暗了我发光 2024-07-15 15:52:44

在您的情况下,将 preg_replace/e 标志一起使用要简单得多:

$content = preg_replace(
    '/[\x80-\xff]/e',
    '"&#".ord($0).";"',
    $content);

It's a lot simpler to use preg_replace with the /e flag in your case:

$content = preg_replace(
    '/[\x80-\xff]/e',
    '"&#".ord($0).";"',
    $content);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文