473,786 Members | 2,578 Online
Bytes | Software Development & Data Engineering Community
+ 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\.We bsite\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:.*?_g ametype\\(.*?)\ \.*?mapname\\(. *?)\\/) {...

It took me quite long time, to find this ugly construct in PHP:
$match_initGame = '(\d+:\d+)\s*In itGame:.*?_game type' . preg_quote("\\" )
.. '(.*?)' . preg_quote("\\g amename") . '.*?' . preg_quote("\\m apname\\") .
'(.*?)' . 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 5569
Thorsten Walenzyk wrote:
I have a line like this:
"329:47 InitGame:
\.Admin\xxx\.We bsite\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\.W ebsite\www.yyy.de\g_gametype\hq\gamename\Call of '
. 'Duty\mapname\m p_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($r x, $text, $matches)) {
exit("Not found!\n");
}

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

?>

--
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\.We bsite\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\.W ebsite\www.yyy.de\g_gametype\hq\gamename\Call of '
. 'Duty\mapname\m p_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($r x, $text, $matches)) {
exit("Not found!\n");
}

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

?>

--
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
4246
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 gifs). then it processes them based off of which image type it selected, either the st- 's or the sb- 's.
2
4697
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 = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
2
2975
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: if (strcmp(strtolower($_POST), 'apply') != 0) { // GO BACK TO grad_application with $_SESSION session_start(); if (sizeof($_POST) > 0) $_SESSION = $_POST; if (sizeof($_GET) > 0) $_SESSION = $_GET; header("Location:...
22
2775
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, $results)
5
7538
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, $res_description); if (!$valid_srch) { ...
6
10252
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", $_FILES) I understand the basic preg_match but am confused as to how the string pattern part is working i.e. "/\." . $allowed_file_types . "$/i"
2
4255
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
5373
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: preg_match( '/^+$/', $TestString ); OK everything seems fine. However, did you know if you pass the following to preg_match: "12345\n" it will return that a match occurred?!? Even though the newline is not a valid character in our regular...
8
3998
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' ='~^.~' ); // make sure there is always a match
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9496
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5534
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
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.