trouble with preg_match

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Hi all,

I'm pretty new to PHP. So far I know regular expression only from Perl

I've written a perl script which I now want to port to PHP. It works in
PErl, but gives my a headache in PHP.

I have a line like this:
"329:47 InitGame:
\.Admin\xxx\.Website\\g_gametype\hq\gamename\Call of
I want to get the time at the beginning of the line. Further I need the text
between  "gametype\" and "\gamename" ("hq" in this case) and the text
between "mapname\" and "\protocol" ("mp_ship").

The reg ex in Perl looks like this:
    if  ($line =~
m/(\d+:\d+)[\s]*InitGame:.*?_gametype\(.*?)\.*?mapname\(.*?)\/)  {...

It took me quite long time, to find this ugly construct in PHP:
    $match_initGame = '(\d+:\d+)\s*InitGame:.*?_gametype' . preg_quote("\")
.. '(.*?)' .  preg_quote("\gamename") . '.*?' . preg_quote("\mapname\") .
'(.*?)' . preg_quote ("\");
    if  (preg_match("/$match_initGame/", $line, $matches ) )  {...

Is there is any better way to solve my problem?

Thanks a lot, Thorsten

Re: trouble with preg_match

Thorsten Walenzyk wrote:
Quoted text here. Click to load it


$text = '329:47 InitGame:'
      . '\.Admin\xxx\.Website\\g_gametype\hq\gamename\Call of '
      . 'Duty\mapname\mp_ship\protocol\...';

$rx = '/
         (\d+:\d+)    # get the time
         .*           # ignore any number of anything
         gametype     # up to "gametype"
         \\         # followed by a backslash       (*)
         (.*)         # get the "gametype"             |
         \\         # ignore backslash              (*)
         gamename     # and "gamename"                 |
         .*           # ignore any number of anything  |
         mapname\\  # up to "mapname\"              (*)
         (.*)         # get the "mapname"              |
         \\protocol # followed by "\protocol"       (*)
       /x';           # with extended syntax           |
// (*)        <----------------------------------------'
// a single \ between single quotes (or double quotes) is the
// escape character. As you want a real backslash you have to
// double it.
// But that will get passed as a escape character to the preg_match()
// function! To pass a real backslash to the function, add another
// pair of backslashes and you're all set.

if (!preg_match($rx, $text, $matches)) {
  exit("Not found!\n");



Mail to my "From:" address is readable by all at /
== ** ## !! ------------------------------------------------ !! ## ** ==
may bypass my spam filter. If it does, I may reply from another address!

Re: trouble with preg_match

Thanks Pedro.

It is somehow strange to use 4 backslashes, but it works.

Thanks, Thorsten

Quoted text here. Click to load it

Re: trouble with preg_match

Quoted text here. Click to load it

   You may want to know the difference between single quote and double
quote <

  <?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com    Blog: /

Site Timeline