另一个 php ereg 修复

发布于 2024-09-03 09:13:45 字数 670 浏览 4 评论 0原文

我需要从 ereg 到 preg_match 进行一小段编码。这是代码。

function be_file_list($d, $x) {
    foreach (array_diff(scandir($d), array('.', '..')) as $f) {
        if (is_file($d . '/' . $f) && (($x) ? ereg($x.'$',$f) : 1)) {
            $l[] = $f;
        }
    }

    return $l;
}

这段代码可以按预期工作,即使它看起来不太漂亮 (来源:http://www.php.net/manual/en/function .scandir.php),

但由于 ereg 已被弃用,我真的很想让它成为 preg_match 或类似的东西。

我整个下午都在搞乱这个,电脑快要被淘汰了。我本以为这

preg_match("/"$x.'$',$f"/")

会起作用,但没有骰子。

任何帮助都会很棒。

干杯 本

I have a small chunk of coding I need to take from ereg to preg_match. Here is the code.

function be_file_list($d, $x) {
    foreach (array_diff(scandir($d), array('.', '..')) as $f) {
        if (is_file($d . '/' . $f) && (($x) ? ereg($x.'

This code works as expected even if it doesn't look too pretty
(source: http://www.php.net/manual/en/function.scandir.php)

but as ereg is deprecated, I would really like to make it preg_match, or something like that.

I have been messing with this all afternoon and the PC is about to go out the window. I would have thought that

preg_match("/"$x.'

would have worked but no dice.

Any help would be great.

Cheers
Ben

,$f) : 1)) { $l[] = $f; } } return $l; }

This code works as expected even if it doesn't look too pretty
(source: http://www.php.net/manual/en/function.scandir.php)

but as ereg is deprecated, I would really like to make it preg_match, or something like that.

I have been messing with this all afternoon and the PC is about to go out the window. I would have thought that


would have worked but no dice.

Any help would be great.

Cheers
Ben

,$f"/")

would have worked but no dice.

Any help would be great.

Cheers
Ben

,$f) : 1)) { $l[] = $f; } } return $l; }

This code works as expected even if it doesn't look too pretty
(source: http://www.php.net/manual/en/function.scandir.php)

but as ereg is deprecated, I would really like to make it preg_match, or something like that.

I have been messing with this all afternoon and the PC is about to go out the window. I would have thought that

would have worked but no dice.

Any help would be great.

Cheers
Ben

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

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

发布评论

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

评论(2

短暂陪伴 2024-09-10 09:13:45

你完全错了......尝试

preg_match('/'.preg_quote($x, '/').'$/', $f)

你写道:

preg_match("/"$x.'

你需要在字符串和变量之间使用 . ,并且你的尾部斜杠位于错误的位置。它需要在模式之后,而不是在主题之后。

,$f"/")

你需要在字符串和变量之间使用 . ,并且你的尾部斜杠位于错误的位置。它需要在模式之后,而不是在主题之后。

You've got it all wrong... try

preg_match('/'.preg_quote($x, '/').'$/', $f)

You wrote:

preg_match("/"$x.'

You need .s between the strings and vars, and you've got your trailing slash in the wrong place. It needs to be after the pattern, not after the subject.

,$f"/")

You need .s between the strings and vars, and you've got your trailing slash in the wrong place. It needs to be after the pattern, not after the subject.

怪我太投入 2024-09-10 09:13:45

看起来 glob 就是您要找的东西。结合目录读取和正则表达式

Looks like glob is the thing you're looking for. Combining both directory reading and regular expressions

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