-1

Recently I saw a problem in a simple code:

$regexp = "^((http://)|(https://)|(mailto:)|(ftp://))?(([a-zA-Z0-9_.!~*'()-]|(%[0-9a-fA-F]{2})|[;&=+$,])+(:([a-zA-Z0-9_.!~*'()-]|(%[0-9a-fA-F]{2})|[;:&=+$,])+){0}@){0}((((((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9]))\.){3}((2(([0-4][0-9])|(5[0-5])))|([01]?[0-9]?[0-9]))))|(([a-zA-Z0-9](([a-zA-Z0-9-]{0,62})[a-zA-Z0-9])?\.)*([a-zA-Z](([a-zA-Z0-9-]*)[a-zA-Z0-9])?)))?(:(([0-5]?[0-9]{1,4})|(6[0-4][0-9]{3})|(65[0-4][0-9]{2})|(655[0-2][0-9])|(6553[0-5])))?(/((;)?([a-zA-Z0-9_.!~*'()-]|(%[0-9a-fA-F]{2})|[:@&=+$,])+(/)?)*)?(\?([;/?:@&=+$,]|[a-zA-Z0-9_.!~*'()-]|(%[0-9a-fA-F]{2}))*)?(#([;/?:@&=+$,]|[a-zA-Z0-9_.!~*'()-]|(%[0-9a-fA-F]{2}))*)?$";
$urladdr = '89.221.92.122/api/xml';
$result = eregi($regexp, $urladdr);

wamp server returns true, while xampp server returns false! I know that eregi is deprecated, but right know I expect it to work fine in php 5.3.1

My Question

Is there any php or apache config caused this behavior?

A simple test file of the script is uploaded here: eregi on xampp + phpinfo
The above code in my wamp is here: eregi on wamp + phpinfo

I don't want you to solve my problem, I just want to know why this kind of problems happen.

hakre
  • 193,403
  • 52
  • 435
  • 836
hpaknia
  • 2,769
  • 4
  • 34
  • 63
  • I know, but in wamp -php 5.3.8- (its greater than 5.3.1) it works fine. – hpaknia Jul 25 '13 at 18:36
  • it works, but treat it like a car with the gas warning light on. It could cough and die on you at ANY time. – Marc B Jul 25 '13 at 18:37
  • 1
    thanks guys, I apologize if my question bothered you, especially the angry guy (the one who down voted!) – hpaknia Jul 25 '13 at 18:44
  • Personally, I think this is a valid question: AFAIK, the extension is not deprecated because it is broken, but because it duplicates functionality which is better-maintained in PCRE. So behaviour varying between two environments is still something to examine, even if it turns out to be a known bug. – IMSoP Jul 25 '13 at 21:19
  • Here's a demo on 3v4l.org, showing that there is no difference between PHP *versions* http://3v4l.org/glShp (Note that the code in the question is currently broken as it has unescaped single quotes in a single-quoted string; I backslash-escaped these to make it compile.) – IMSoP Jul 25 '13 at 21:24
  • You are right, I corrected the code in the question. – hpaknia Jul 26 '13 at 04:14

2 Answers2

3

It appears that the POSIX "extended regex" module/extension can either take advantage of native system support or use an implementation bundled with the PHP source. I haven't found any clear reference to this, but there are clues:

  • In the extension's "Installation" instructions, an option is described for which of 3 implementations to compile in. I note that one of the options is "apache", implying that even under Windows, a "third-party" implementation might be available.
  • You can browse the source code for where the implementations are actually loaded, e.g. the series of if statements in ext/ereg/php_regex.h
  • The phpinfo() output you posted includes the line "Bundled library enabled". (Incidentally, I came upon the source which outputs that: ext/ereg/ereg.c line 238.

This is actually quite common for many of the older parts of PHP, which were originally built on system libraries and APIs which can reliably be found on "POSIX-compliant" systems, and were later "ported" to other systems by reimplementing the required functionality. This causes some discrepancies between platforms, particularly on Windows, where most of the expected libraries will be missing.

Recent versions of PHP have tried to identify and rectify such inconsistencies - for instance, many of the filesystem-related functions used to have warnings of limited or incorrect behaviour under Windows, but now show these as fixed in a particular version. However, since this particular extension has been deprecated in favour of the cross-platform PCRE library, there has probably not been a great deal of testing of its different implementations, even before the official deprecation notice was added in PHP 5.3.

IMSoP
  • 89,526
  • 13
  • 117
  • 169
  • Thanks @IMSoP, is there any way to change xampp php ereg defenition file with another php version? – hpaknia Jul 26 '13 at 04:32
  • Unfortunately this is an option set while PHP is being built from source, so it is likely to be rather tricky to change it (it would somewhat defeat the purpose of a pre-built stack like xampp). I think as others have said, you are better off spending the effort converting your code to use PCRE. – IMSoP Jul 26 '13 at 08:16
  • Thanks, You have a really good knowledge in php. – hpaknia Jul 26 '13 at 08:39
1

eregi is deprecated! Use preg instead.

See: How can I convert ereg expressions to preg in PHP?

Community
  • 1
  • 1
Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • Thanks, so I conclude that deprecated functions are not reliable? isn't it? – hpaknia Jul 25 '13 at 20:25
  • @HPM: Deprecated functions are definitely not reliable. If a function is deprecated in x.y, it's very likely to be removed in x.y+1. – Madara's Ghost Jul 25 '13 at 20:26
  • @MadaraUchiha That is certainly one definition of "not reliable". However, it does not mean that a deprecated function is "not reliable" *within a particular version*, as appears to be the case here. – IMSoP Jul 25 '13 at 21:35
  • 1
    @IMSoP: Why does it matter? Don't use deprecated functions, especially ones that have a very easy, very clear alternative. It's like asking "Why does my WWI rifle not work anymore? I want to join the military with it". – Madara's Ghost Jul 26 '13 at 08:07
  • @MadaraUchiha Your analogy is flawed - a WWI rifle is quite likely not to work *because it is old*, which is not the problem here. I agree that the best *solution* is to move to PCRE, but that doesn't actually explain the *problem* as such. – IMSoP Jul 26 '13 at 08:14