...

View Full Version : Need help with a regex



jewishj
06-01-2007, 10:17 AM
I need some help with a regex. I need it to pull the data from a specific content in a language file which details info about the language file.

The comment looks like this:



/************************************************
LANGUAGE FILE
SITE: MySite
LANGUAGE: English
FILE: en.php
************************************************/


I need to get the information so that I can easily figure out that SITE = MySite, LANGAUGE = English, and so on. So I would either need an array like this:

array(


array('SITE','MySite'),
array('LANGAUGE','English),
etc.

);

or something like this:

array('SITE: MySite', 'LANGUAGE: English', etc.);

Right now I have a regex which does not work at all - it returns just an empty array when used in conjunction with preg_match_all(). The regex I have come up with so far is:



$pattern="/^(\/[\*]*)(^LANGUAGE\sFILE)(^.*)*(^[\*]*\/*)/smU";


Thanks in advance for any help in fixing this regex or in pointing me in the right direction.

printf
06-01-2007, 12:55 PM
I don't know exactly where your getting the string, but if it's from a file, you could do something like...

// there is no checking (file exists, is readable), as it's just example


<?php

function get_comment_array ( $file, $data = '', $find = '/' )
{
$io = fopen ( $file, 'rb' );

do
{
$data .= fgets ( $io, 4096 );

} while ( substr_count ( $data, $find ) != 2 && ! feof ( $io ) );

fclose ( $io );

preg_match_all ( '/(\w+)\s?\:\s?([a-z0-9\.]+)\r?\n/is', $data, $m );

return array_combine ( $m[1], $m[2] );
}

$file = './some_file.php';

$array = get_comment_array ( $file );

print_r ( $array );

?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum