ereg_replace 到 preg_replace?

发布于 2024-08-25 06:44:32 字数 161 浏览 4 评论 0原文

我怎样才能转换

ereg_replace(".*\.(.*)$","\\1",$imgfile);

preg_replace... ?

我有麻烦吗?

How can I convert

ereg_replace(".*\.(.*)$","\\1",$imgfile);

to

preg_replace... ?

?

I'm having trouble with it?

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

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

发布评论

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

评论(3

偷得浮生 2024-09-01 06:44:32

将 ereg 模式移植到 preg 时,您应该了解 4 个主要知识:

  1. 添加分隔符(/): 'pattern' =>; '/pattern/'

  2. 转义分隔符(如果它是模式的一部分):'patt/ern' =>
    '/patt\/ern/'

    通过以下方式以编程方式实现它:
    $ereg_pattern = '

    .+

    ';
    $preg_pattern = '/' .addcslashes($ereg_pattern, '/') 。 '/';

  3. eregi(不区分大小写匹配):'pattern' =>; '/pattern/i' 所以,如果
    您正在使用 eregi 函数进行不区分大小写的匹配,只需添加
    'i' 位于新模式('/pattern/')的末尾。

  4. ASCII 值:在 ereg 中,如果在模式中使用数字,则它是
    假设您指的是字符的 ASCII。但在
    preg, number 不被视为 ASCII 值。所以,如果你的模式
    ereg 表达式中包含 ASCII 值(例如:换行符、
    制表符等),然后将其转换为十六进制并以 \x 为前缀。
    示例:9(tab) 变为 \x9 或使用 \t。

希望这会有所帮助。

You should know 4 main things to port ereg patterns to preg:

  1. Add delimiters(/): 'pattern' => '/pattern/'

  2. Escape delimiter if it is a part of the pattern: 'patt/ern' =>
    '/patt\/ern/'

    Achieve it programmatically in following way:
    $ereg_pattern = '<div>.+</div>';
    $preg_pattern = '/' .addcslashes($ereg_pattern, '/') . '/';

  3. eregi(case-insensitive matching): 'pattern' => '/pattern/i' So, if
    you are using eregi function for case insenstive matching, just add
    'i' in the end of new pattern('/pattern/').

  4. ASCII values: In ereg, if you use number in the pattern, it is
    assumed that you are referring to the ASCII of a character. But in
    preg, number is not treated as ASCII value. So, if your pattern
    contain ASCII value in the ereg expression(for example: new line,
    tabs etc) then convert it to hexadecimal and prefix it with \x.
    Example: 9(tab) becomes \x9 or alternatively use \t.

Hope this will help.

宁愿没拥抱 2024-09-01 06:44:32
preg_replace("/.*\.(.*)$/", "\\1", "foo.jpg")

我不知道为什么 PHP 需要 / 分隔符。 Perl、JS 等拥有它们的唯一原因是它们允许正则表达式文字,而 PHP 不允许。

preg_replace("/.*\.(.*)$/", "\\1", "foo.jpg")

I don't know why PHP requires the / delimiters. The only reason Perl, JS, etc. have them is that they allow regex literals, which PHP doesn't.

时光无声 2024-09-01 06:44:32

分隔符,将任何字符添加到表达式的开头和结尾,在本例中,按照传统,“/”字符 preg_replace('/.*\.(.*)$/',"\\1", $imgfile); 正则表达式不是很好,最好使用 strrpos 并采用 substr()。

正则表达式很慢,使用这个。
$extension=substr($imgName,strrpos($imgName,'.'));

delimiters, add any char to beginning and end of expression, in this case, and by tradition, the '/' character preg_replace('/.*\.(.*)$/',"\\1",$imgfile); The regex isn't very good, better to use strrpos and take substr().

Regex is slow, use this.
$extension=substr($imgName,strrpos($imgName,'.'));

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