Welcome to Dream.In.Code
Getting PHP Help is Easy!

Join 136,487 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,698 people online right now. Registration is fast and FREE... Join Now!




Regex - Problem, or bug?

 
Reply to this topicStart new topic

Regex - Problem, or bug?, Simple help needed, i'm sure i've overlooked something.

AP-Richard
29 Jan, 2008 - 04:30 PM
Post #1

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 3

Hello,

First of all - I'm quite new to Regex, but knowing the amount of work it could save me when programming, I thought I should learn it.

Well.. I came across a problem quite quickly. First of all, lets get to my code.

The Code

CODE
<?php
$xml = "<game id='123'>Testing, 123, 1</game> <game id='456'>Testing, 456, 1</game>";

preg_match_all("/<game id='[0-9]+'>.+<\/game>/", $xml, $matches);

print_r($matches);


The Output

QUOTE
Array
(
[0] => Array
(
[0] => <game id='123'>Testing, 123, 1</game> <game id='456'>Testing, 456, 1</game>
)

)



The Problem

Well, as you can see, I expected it, to cut it at </game>, as thats what I put in my regex (/<game id='[0-9]+'>.+<\/game>/), but it just carried on, till it got to the last </game> (I assume?)

Can anyone spread some light on this problem? as I'm SURE its something i've done wrong.

Thanks,
Richard
User is offlineProfile CardPM
+Quote Post

AP-Richard
RE: Regex - Problem, Or Bug?
29 Jan, 2008 - 04:55 PM
Post #2

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 3

Hello,

Expected output would be:

CODE
Array
[0] => Array
     [0] => <game id='123'>Testing, 123, 1</game>
     [1] => <game id='456'>Testing, 456, 1</game>


Thanks

This post has been edited by AP-Richard: 29 Jan, 2008 - 04:56 PM
User is offlineProfile CardPM
+Quote Post

AP-Richard
RE: Regex - Problem, Or Bug?
29 Jan, 2008 - 07:20 PM
Post #3

New D.I.C Head
*

Joined: 29 Jan, 2008
Posts: 3

Hello,

Solved. I changed the regex to:

/<game id='[0-9]+'>.*?<\/game>/s
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 07:32PM

Live PHP Help!

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month