Extracting info from a line:

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Extracting info from a line:

Post by Todd_Z »

I have a text file which contains lines with the following form.

Code: Select all

(9911)		05/10/05	1-2-7-4
What I would like to have as a result:

Code: Select all

Array
(
    ї0] => (9919)		05/18/05	8-0-0-9
    ї0] => 9919
    ї1] => 05/18/05
    ї2] => Array
        (
            ї0] => 8
            ї1] => 0
            ї2] => 0
            ї3] => 9
        )

)

Code: Select all

$regex = &quote;#^\((\d{1,4})\)\t\t(\d\d/\d\d/\d\d)\t((\d)-(\d)-(\d)-(\d))#&quote;;
preg_match( $regex, $pick, $parts );

Code: Select all

Array
(
    ї0] => (9919)		05/18/05	8-0-0-9
    ї1] => 9919
    ї2] => 05/18/05
    ї3] => 8-0-0-9
    ї4] => 8
    ї5] => 0
    ї6] => 0
    ї7] => 9
)
Can I somehow make a 2d array with a preg_match pattern?
R0d Longfella
Forum Newbie
Posts: 20
Joined: Fri Apr 08, 2005 7:17 am

Post by R0d Longfella »

Why don't you just put the preg_match in a wrapper funtion, and then fill in your own array with the match results. Easy as that!

Code: Select all

function myVeryOwnWrapperFunction ($pick) {
  $regex = "#^\((\d{1,4})\)\t\t(\d\d/\d\d/\d\d)\t((\d)-(\d)-(\d)-(\d))";
  preg_match( $regex, $pick, $parts );
  return array (
           $parts[0],
           $parts[1],
           $parts[2],
           array (
             $parts[4],
             $parts[5],
             $parts[6],
             $parts[7]
             )
           );
}
User avatar
Todd_Z
Forum Regular
Posts: 708
Joined: Thu Nov 25, 2004 9:53 pm
Location: U Michigan

Post by Todd_Z »

Sounds like a plan, i was just hoping that there was some super cool regex trick to make a multidimensional array :?
Post Reply