Jump to content

Recommended Posts

Hello again, trying to use preg_match and I am stupid I have use the example from php.net and it failed and tryed different examples from other sites, still having the same issue.

 

I am use to using the programming language Turing, I have only studied it in grade 10 and the school year has ended and I am waiting till September to get back into the class. So I though I would try and learn PHP over the summer time.

 

I am trying to use an array with the beginning and last chunks of text to find the data between it so I used an array to hold those pieces of data call them back with the help of a for statement. I try setting up the preg_match and keeps returning an error message to me.

 

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in D:\wamp\www\RSPS\test.php  on line 12

 

If someone could help me that would be greatful. The next steps to get this script working is to put the data preg_match has found back into an array and display it back so I can call it back like this. Where ever I need it to be displayed.

 

Thanks

~AJ

 

<?php echo $matches[1]; ?>

 

<?php
$data = file_get_contents('Matthew.txt');

$firstcol = array("username = ","skill = 0	");
$secondcol = array("character-password","	");

for ($i = 0; $i <= 1; $i++) {

$a = ''.$firstcol[$i].'(.+?)'.$secondcol[$i].'';
$a = preg_quote($a, '*');

preg_match_all($a,$data,$matches); 
echo $matches;
}
?>

 

Matthew.txt

[ACCOUNT]
character-username = Matthew
character-password = 53552e2db2937526b0766ce1e1eb1ffd

[CHARACTER]
character-height = 0
character-posx = 2558
character-posy = 3090
character-rights = 3
character-lastconnection = 
character-special = 4225
character-tz = -1
character-starter = 1
character-pouch1 = 0
character-pouch2 = 0
character-pouch3 = 0
character-pouch4 = 0
character-energy = 100
character-expLock = false
character-taskID = -1
character-taskAmount = -1
character-jad = 1
character-Ahrim = 0
character-ce = 0
character-sp = 0
character-aa = 0
character-Verac = 0
character-Dharok =0
character-Torag = 0
character-Karils =0
character-Guthan =0
character-Barrows 0
character-brightness = 0
character-fighttype = 3
character-skill = 3
character-chat = 1
character-ancients = 2
character-pin = 0
character-pkPoints = 1059
character-saradomin = 0
character-zammykills =60
character-retaliate = 0
character-action = 0
character-pcpoints = 799
character-duelrating0
character-duelpoints0
character-assault = 1
character-donator = 1
character-bow = 100
character-shield = 100
character-tradeTimer = 0
character-tb = 0
character-q15 = 0
character-q14 = 0
character-q13 = 0
character-q12 = 0

[EQUIPMENT]
character-equip = 0	6182	1	
character-equip = 1	4359	1	
character-equip = 2	1725	1	
character-equip = 3	14013	1	
character-equip = 4	6916	1	
character-equip = 5	-1	0	
character-equip = 6	-1	0	
character-equip = 7	2497	1	
character-equip = 8	-1	0	
character-equip = 9	7461	1	
character-equip = 10	2904	1	
character-equip = 11	-1	0	
character-equip = 12	-1	0	
character-equip = 13	-1	0	

[LOOK]
character-look = 0	0
character-look = 1	3
character-look = 2	14
character-look = 3	18
character-look = 4	26
character-look = 5	33
character-look = 6	36
character-look = 7	42
character-look = 8	0
character-look = 9	1
character-look = 10	2
character-look = 11	0
character-look = 12	0
character-look = 13	0
character-look = 14	0
character-look = 15	0
character-look = 16	0
character-look = 17	0
character-look = 18	0

[sKILLS]
character-skill = 0	99	28673300
character-skill = 1	99	28675400
character-skill = 2	99	32294500
character-skill = 3	99	32300555
character-skill = 4	99	28000000
character-skill = 5	99	28263340
character-skill = 6	99	28000000
character-skill = 7	99	28000000
character-skill = 8	99	28000000
character-skill = 9	99	28000000
character-skill = 10	99	28000000
character-skill = 11	99	28000000
character-skill = 12	99	28000000
character-skill = 13	99	28020000
character-skill = 14	99	28007000
character-skill = 15	99	28000000
character-skill = 16	99	28000000
character-skill = 17	99	29342795
character-skill = 18	99	28000000
character-skill = 19	99	28000000
character-skill = 20	99	28000000
character-skill = 21	1	0
character-skill = 22	1	0
character-skill = 23	1	0
character-skill = 24	0	0

[iTEMS]
character-item = 0	9002	1
character-item = 1	1276	1

[bANK]
character-bank = 0	4358	1
character-bank = 1	4336	2
character-bank = 2	14012	3
character-bank = 3	344	1
character-bank = 4	4326	1
character-bank = 5	14058	1
character-bank = 6	4332	1
character-bank = 7	4338	1
character-bank = 8	14014	2
character-bank = 9	4328	1
character-bank = 10	4362	1
character-bank = 11	4086	1
character-bank = 12	4334	1
character-bank = 13	14063	1
character-bank = 14	4342	2
character-bank = 15	4318	1
character-bank = 16	7612	8
character-bank = 17	4340	1
character-bank = 18	2435	27
character-bank = 19	4344	2
character-bank = 20	4348	1
character-bank = 21	4352	1
character-bank = 22	4330	1
character-bank = 23	2348	1
character-bank = 24	1334	1
character-bank = 25	996	2000045851
character-bank = 26	386	1013
character-bank = 27	4320	2
character-bank = 28	4360	1
character-bank = 29	4346	1
character-bank = 30	4322	1
character-bank = 31	4354	1
character-bank = 32	551	1
character-bank = 33	1541	1
character-bank = 34	747	1
character-bank = 35	2905	1
character-bank = 36	2492	1
character-bank = 37	6819	1
character-bank = 38	4566	1
character-bank = 39	4356	1
character-bank = 40	563	3010
character-bank = 41	557	5000
character-bank = 42	555	4000
character-bank = 43	566	4000
character-bank = 44	561	3000
character-bank = 45	556	4000
character-bank = 46	1049	1
character-bank = 48	4152	1
character-bank = 49	1043	1
character-bank = 50	651	1
character-bank = 51	641	1
character-bank = 52	6542	1
character-bank = 53	4098	1
character-bank = 54	4096	1
character-bank = 55	14047	1
character-bank = 56	14029	1
character-bank = 57	14051	1
character-bank = 58	14032	1
character-bank = 59	9005	1
character-bank = 60	14031	2
character-bank = 61	14067	1
character-bank = 62	14066	1
character-bank = 63	14039	1
character-bank = 64	14041	1
character-bank = 65	14064	1
character-bank = 66	7807	1
character-bank = 67	7808	1
character-bank = 68	7450	1
character-bank = 69	14049	1
character-bank = 70	4676	1
character-bank = 71	14040	1
character-bank = 72	4068	2
character-bank = 73	14071	1
character-bank = 74	14070	1
character-bank = 75	6571	1
character-bank = 76	14054	1
character-bank = 78	14050	1
character-bank = 79	14065	1
character-bank = 80	14061	1
character-bank = 81	14010	1
character-bank = 82	14053	1

[FRIENDS]
character-friend = 0	3458605949762185758
character-friend = 1	904262259
character-friend = 2	918897639
character-friend = 3	41091004572
character-friend = 4	19021682
character-friend = 5	453997938233492
character-friend = 6	59658913159
character-friend = 7	3470371525163566
character-friend = 8	279971649
character-friend = 9	601731966

[iGNORES]

[EOF]

Link to comment
https://forums.phpfreaks.com/topic/207214-preg-match-delimiter/
Share on other sites

Or for easier reading:

$a = '/{$firstcol[$i]}(.+?){$secondcol[$i]}/";

 

Also, $matches will be an array, so you can't echo it out. You will want to use the second index (i.e. $matches[1] to see the results of the matches). So, use print_r($matches[1]) to see the results. Check the documentation to see what is included in the $matches parameter.

 

Here is what I would do:

$data = file_get_contents('Matthew.txt');

$delimiters = array(
    "username = |character-password",
    "skill = 0	|	"
);

foreach($delimiters as $delimSet)
{
    $delimPair = split('|', $delimSet);
    $pattern = preg_quote("/{$delimPair[0]}(.+?){$delimPair[1]}/", '*');
    preg_match_all($pattern, $data, $matches); 
    print_r($matches[1]);
}

I tried using that code you posted and returned errors:

 

Deprecated: Function split() is deprecated in D:\wamp\www\RSPS\test.php  on line 11

Warning: split() [function.split]: REG_EMPTY in D:\wamp\www\RSPS\test.php on line 11

Notice: Undefined offset: 1 in D:\wamp\www\RSPS\test.php on line 14

Deprecated: Function split() is deprecated in D:\wamp\www\RSPS\test.php on line 11

Warning: split() [function.split]: REG_EMPTY in D:\wamp\www\RSPS\test.php on line 11

Notice: Undefined offset: 1 in D:\wamp\www\RSPS\test.php on line 14

 

[attachment deleted by admin]

OK, I meant to use explode() not split()  - I was thinking of Javascript.

 

Anyway, I think there are tow problems.

 

1. The preg_quote is escaping the control characters in the expression. It should be run on the variables only.

 

2. The expression will need to use the "s" flag to have the expression work across multiple lines.

 

$delimiters = array(
    "username = |character-password",
    "skill = 0	|	"
);

foreach($delimiters as $delimSet)
{
    $delimPair  = explode('|', $delimSet);
    $startDelim = preg_quote($delimPair[0], '*');
    $endDelim   = preg_quote($delimPair[1], '*');
    $pattern    = "/{$startDelim}(.+?){$endDelim}/s";
    preg_match_all($pattern, $data, $matches); 
    print_r($matches[1]);
}

 

I don't under stand delimiters  :shrug:

 

A delimiter is just a character you choose to specify the start and end of your pattern.  It can be most any non-alphanumeric character, though most people use / ~ or # (I personally like ~). 

 

So basically you would have "~your actual pattern here~".  The reason why you need delimiters in php is because pattern modifiers are also contained within the argument.  For example, if I wanted to make my pattern case-insenstive, I would do this:

 

"~pattern~i"

 

so php needs to know the boundaries of your actual pattern so it can know if you are using any modifiers. 

 

NOTE: whatever delimiter you choose to use in your pattern...if you need to match that character in your pattern you will need to escape it (prefix it with a backslash) so that php knows you are searching for that character instead of ending your pattern. 

 

As a programmer, you already use delimiters in other ways, though you probably just don't realize it.  For instance, quotes around strings are delimiters, specifying the start and end of the string :P

 

I don't under stand delimiters  :shrug:

 

A delimiter is just a character you choose to specify the start and end of your pattern.  It can be most any non-alphanumeric character, though most people use / ~ or # (I personally like ~). 

 

So basically you would have "~your actual pattern here~".  The reason why you need delimiters in php is because pattern modifiers are also contained within the argument.  For example, if I wanted to make my pattern case-insenstive, I would do this:

 

"~pattern~i"

 

so php needs to know the boundaries of your actual pattern so it can know if you are using any modifiers. 

 

NOTE: whatever delimiter you choose to use in your pattern...if you need to match that character in your pattern you will need to escape it (prefix it with a backslash) so that php knows you are searching for that character instead of ending your pattern. 

 

As a programmer, you already use delimiters in other ways, though you probably just don't realize it.  For instance, quotes around strings are delimiters, specifying the start and end of the string :P

 

Couldn't I've used that instead since I know my data is the start and end are always the same :o?

Another thing is I have started programming in Grade 10 and it was simple. The class ended 4 months ago and now that school is out I decided to learn something else.

 

Its kind of cool how PHP works compared to Turing (learned at school). To grab text in Turing you would use:

var data:string
get data
put data

 

in PHP it's

echo $_POST['data'];

 

But this requires another page with the fields. But just to process the commands is way different. PHP uses apache with PHP. Turing just compiles it and turns it into binary.

Regular expressions are, in my opinion, one of the more difficult aspects of programming to learn. Trying to intelligently explain it in a forum post is not achievable. Here's a good site with a tutorial and lots of reference info to get you started: http://www.regular-expressions.info/

Couldn't I've used that instead since I know my data is the start and end are always the same :o?

 

I guess you are talking about the quotes? No, like I said, with php, the modifiers are also put within the string, so php needs to know the boundary between your pattern and your modifiers.  Even though modifiers are optional, the delimiters are not. Otherwise there would be no way to tell php that hey these are delims and i have modifiers, vs. no I don't have delims and modifiers, or hey these sure look like delims and modifiers but they aren't, they are normal text to match! IOW in order to prevent ambiguity, the delimiters have to be present no matter what.

I tried this but it returned nothing. I still need to watch all the E Learning Videos lol.

 

$data = file_get_contents('Matthew.txt');

function parser($data)
{
    $pattern = "/username = (.+?)character-password/";
    preg_match($pattern, $data, $matches);
    return $matches[1];
}

01.Introduction [Check]

02.PHP Overview [Check]

03.Mac Installation

04.Windows Installation [Check]

05.First Steps [Check]

06.Exploring Data Types [Check]

07.Control Structures Pt. 1 Logical Expressions

08.Control Structures Pt. 2 Loops

09.User-Defined Functions

10.Debugging

11.Building Web Pages

12.MySQL Basics

13.Building a Content Management System

14.Application CRUD

15.Building the Public Area

16.Conclusion

 

I tried this but it returned nothing. I still need to watch all the E Learning Videos lol.

 

$data = file_get_contents('Matthew.txt');

function parser($data)
{
    $pattern = "/username = (.+?)character-password/";
    preg_match($pattern, $data, $matches);
    return $matches[1];
}

 

As I posted way back on page one, you need the "S" flag to tell the regex parser to look for the pattern across multiple lines

    $pattern = "/username = (.+?)character-password/s";

$data = file_get_contents('Matthew.txt');

    $pattern = "/character-username = (.+?)character-password/s";
    preg_match($pattern, $data, $matches);
    return $matches[1];

 

I tried this and it still returning nothing and I echoed $data and it has the file contents.  :confused:

Still Returns nothing D:

 

$data = file_get_contents('Matthew.txt');

    //$pattern = "/character-username = (.+?)character-password/s";
$pattern = "~character-username = (.+?)$~s";
    preg_match($pattern, $data, $matches);
    return $matches[1];

 

 

 

[attachment deleted by admin]

I'm really not sure what you want from us, because given the file you attached and the code you claim to be using, neither patterns you have tried will return nothing. The first one would return 'Matthew', the second would return most of the entire file (C.V's last idea was perhaps supposed to include the m modifier).

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.