473,471 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

trouble with preg_match

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\www.yyy.de\g_gametype\hq\gamename\Call of
Duty\mapname\mp_ship\protocol\..."
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
Jul 17 '05 #1
3 5550
Thorsten Walenzyk wrote:
I have a line like this:
"329:47 InitGame:
\.Admin\xxx\.Website\www.yyy.de\g_gametype\hq\gamename\Call of
Duty\mapname\mp_ship\protocol\..."
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").

<?php

$text = '329:47 InitGame:'
. '\.Admin\xxx\.Website\www.yyy.de\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");
}

unset($matches[0]);
print_r($matches);

?>

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2
Thanks Pedro.

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

Thanks, Thorsten
"Pedro Graca" <he****@dodgeit.com> schrieb im Newsbeitrag
news:sl*******************@ID-203069.user.uni-berlin.de...
Thorsten Walenzyk wrote:
I have a line like this:
"329:47 InitGame:
\.Admin\xxx\.Website\www.yyy.de\g_gametype\hq\gamename\Call of
Duty\mapname\mp_ship\protocol\..."
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").

<?php

$text = '329:47 InitGame:'
. '\.Admin\xxx\.Website\www.yyy.de\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");
}

unset($matches[0]);
print_r($matches);

?>

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!

Jul 17 '05 #3
"Thorsten Walenzyk" <th***************@gmx.de> wrote in message news:<co*************@news.t-online.com>...
Thanks Pedro.

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


You may want to know the difference between single quote and double
quote <http://in2.php.net/types.string>

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100...
2
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out...
2
by: Phil Powell | last post by:
I have a form that will be preserving form data prior to processing the form data. Upon clicking a certain submit button you will go to another PHP script that will contain the following code: ...
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
6
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i",...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
13
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this:...
8
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.