• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1401
  • Last Modified:

Compilation failed: unrecognized character after (?

This might be a bit random for people but I'm using CakePHP (cakephp.org) and CakeAMFPHP which is Cake's version of the open source flash remoting project AMFPHP (amfphp.org).

The error I'm getting is

Compilation failed: unrecognized character after (?

the line of code is

elseif (preg_match('/(?P<direction>\\x20ASC|\\x20DESC)/i', $keys, $match))

from

function order ($keys, $direction = 'ASC')
    {
        if (empty($keys))
        {
            return '';
        }
        if(is_array($keys))
        {
            foreach($keys as $key => $value)
            {
                if(is_numeric($key))
                {
                    $key = $value;
                    $value = ' '.$direction;
                }
                else
                {
                    $value= ' '.$value;
                }
                $order[] = $this->name($key).$value;
            }
            return ' ORDER BY '.join(',', $order);
        }
        else
        {
            $keys = preg_replace('/ORDER\\x20BY/i', '', $keys);
            if (strpos('.', $keys))
            {
                preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result, PREG_PATTERN_ORDER);
                $pregCount = count($result[0]);

                for ($i = 0; $i < $pregCount; $i++)
                {
                    $keys = preg_replace('/'.$result[0][$i].'/', $this->name($result[0][$i]), $keys);
                }
                if (preg_match('/\\x20ASC|\\x20DESC/i', $keys))
                {
                    return ' ORDER BY '.$keys;
                }
                else
                {
                    return ' ORDER BY '.$keys.' '.$direction;;
                }
            }
            elseif (preg_match('/(?P<direction>\\x20ASC|\\x20DESC)/i', $keys, $match))
            {
                $direction = $match['direction'];
                $keys = preg_replace('/'.$match['direction'].'/', '', $keys);
                return ' ORDER BY '.$keys.$direction;
            }
            else
            {
                $direction = ' '.$direction;
            }
            return ' ORDER BY '.$keys.$direction;
        }
    }

the Cake file is /cake/libs/model/datasources/dbo_source.php.

thanks
0
vixiom
Asked:
vixiom
  • 4
  • 2
1 Solution
 
TomeeboyCommented:
Not sure if you can have parenthesis in the regex like that.. hm.. Try this:

elseif (preg_match('/\(?P<direction>\\x20ASC|\\x20DESC\)/i', $keys, $match))
0
 
TomeeboyCommented:
Err.. sorry, memory lapse there.  I think it's considering (?P to be a regex option, but not completely sure (I'm not the greatest with regex).  If so, then "P" is not being recognized as a valid attribute for that option I guess.
0
 
TomeeboyCommented:
Okay, beginning to understand the expression a bit more after doing some further digging.  ?P<direction> is attempting to create a backreference "direction" for the capture group (which is defined by the parentheses).  Did you put that regex in there yourself?  Unfortunately, I'm nowhere near good enough with regex to know if that is even supported in PHP (or if there's another way to do it that won't cause an error).  Maybe one of the regex gurus could chime in on this one?
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
vixiomAuthor Commented:
I didn't write any of this, it's a part of the Cake framework, a very specialized part to use AMFPHP. I'm a Flash guy and AMFPHP is how I use Flash remoting with PHP/MySQL, I'm just trying to get the Cake framework going with remoting cause it's going to save me tons of time building apps.

So... long story short I'm most likely worse at regex than you are :)

It's an alpha build of CakeAMFPHP so I've reported the error I just wanted to see if I could fix it.
0
 
TomeeboyCommented:
Heh.  Well, I haven't personally seen that method of backreferencing used in a PHP regex, but that doesn't mean much :)  You could try replacing that elseif statement with this:

elseif (preg_match('/\\x20ASC|\\x20DESC/i', $keys, $match))
            {
                $direction = $match[0];
                $keys = preg_replace('/'.$match[0].'/', '', $keys);
                return ' ORDER BY '.$keys.$direction;
            }

preg_match stops at the first match it finds, and if a 3rd parameter is specified in the function call ($match in this case), it returns the match in the form of an array.  $match[0] is the text that matched the full pattern.  I don't think the above would work any differently than what you originally had (other than the fact that it might actually WORK).  Try it and see if there's any difference :)
0
 
WaielEidCommented:
i checked the source files and truly i didn't see that error there

the function in the files was this

    function order ($keys, $direction = 'ASC')
    {
        if (empty($keys))
        {
            return '';
        }
        if(is_array($keys))
        {
            foreach($keys as $key => $value)
            {
                if(is_numeric($key))
                {
                    $key = $value;
                    $value = ' '.$direction;
                }
                else
                {
                    $value= ' '.$value;
                }
                $order[] = $this->name($key).$value;
            }
            return ' ORDER BY '.join(',', $order);
        }
        else
        {
            $keys = preg_replace('/ORDER\\x20BY/i', '', $keys);
            if (strpos('.', $keys))
            {
                preg_match_all('/([a-zA-Z0-9_]{1,})\\.([a-zA-Z0-9_]{1,})/', $keys, $result, PREG_PATTERN_ORDER);
                $pregCount = count($result['0']);

                for ($i = 0; $i < $pregCount; $i++)
                {
                    $keys = preg_replace('/'.$result['0'][$i].'/', $this->name($result['0'][$i]), $keys);
                }
                if (preg_match('/\\x20ASC|\\x20DESC/i', $keys))
                {
                    return ' ORDER BY '.$keys;
                }
                else
                {
                    return ' ORDER BY '.$keys.' '.$direction;;
                }
            }
            elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match))
            {
                $direction = $match['1'];
                $keys = preg_replace('/'.$match['1'].'/', '', $keys);
                return ' ORDER BY '.$keys.$direction;
            }
            else
            {
                $direction = ' '.$direction;
            }
            return ' ORDER BY '.$keys.$direction;
        }
    }

as you can see the state ment is

        elseif (preg_match('/(\\x20ASC|\\x20DESC)/i', $keys, $match))
            {
                $direction = $match['1'];
                $keys = preg_replace('/'.$match['1'].'/', '', $keys);
                return ' ORDER BY '.$keys.$direction;
            }
not the one you posted
there is no P<direction> in it

0
 
vixiomAuthor Commented:
Getting the latest source fixed it. Doh! I was using an earlier version of cake.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now