Php – A regex I have working in Ruby doesn’t in PHP; what could the cause be


I do not know ruby. I am trying to use the following regex that was generated by ruby (namely by running on the grammar given rfc1738) in php. It is failing to match in php, but it is successfully matching in ruby. Does anyone see what differences between php's and ruby's handling of regexes that might explain this discrepancy?


Since you all love regexes so much, how about an alternate solution.
Given the ABNF in an rfc, I want a way (in php) to check if an arbitrary string is in the grammar.

APG fails to compile on a 64-bit system,

VTC is not Free,

and I have not found any other such tools. I would also prefer not to use a regex, but it's the closest I've come to success.

Best Solution

It would be nice to see a sample of the code you were trying to use in PHP, but try the following (remember to change $_SERVER["HTTP_REFERER"] to your input variable):

if (preg_match('#http://(?:(?:(?:(?:[0-9a-z]|[0-9a-z](?:[\\x2d0-9a-z]?)*[0-9a-z])\\x2e)?)*(?:[a-z]|[a-z](?:[\\x2d0-9a-z]?)*[0-9a-z])|\\d+\\x2e\\d+\\x2e\\d+\\x2e\\d+)(?::\\d+)?(?:/(?:(?:[!\\x24\'-\\x2e0-9_a-z]|%[0-9a-f][0-9a-f]|[&:;=@])?)*(?:(?:/(?:(?:[!\\x24\'-\\x2e0-9_a-z]|%[0-9a-f][0-9a-f]|[&:;=@])?)*)?)*(?:\\x3f(?:(?:[!\\x24\'-\\x2e0-9_a-z]|%[0-9a-f][0-9a-f]|[&:;=@])?)*)?)?#i', $_SERVER["HTTP_REFERER"], $l_Results)) {
    $result = $l_Results[1];
} else {
    $result = "";