Regular expression match and push to array

Hi all,

Been trying to tackle this problem I have for a while now and I’m sure it’s simple but I just can’t get it to work.

Basically I have a file containing some string content:

e.g.

Name: php
Version: 5.1.6
Description: PHP is an HTML-embedded scripting language. PHP attempts to
make it easy for developers to write dynamically generated webpages.

I’d like to use divide the file up based on Name, Version & Description and push each string into a associative array

i.e.

[name] => ‘php’
[version] => ‘5.1.6’
[description] => ‘PHP is an HTML-embedded scripting language. PHP attempts to make it easy for developers to write dynamically generated webpages.’

I’m currently experimenting with preg_match_all to do this which seems to work but only for values which are no longer and a single line. Any value more than one line like Description gets stripped and only the last line sets stored in the array.

Does anyone out there know if I’m doing this correct or is there a better method.

Thanks for your time

Regards

Mark

with preg_match_all you will want to look at the ‘s’ modifier, or it might be easier with preg_split:


$str = 'Name: php
Version: 5.1.6
Description: PHP is an HTML-embedded scripting language. PHP attempts to
make it easy for developers to write dynamically generated webpages.';

$matches = preg_split('#(^|\
)(Name|Version|Description):#', $str, -1, PREG_SPLIT_NO_EMPTY);

echo '<pre>';
print_r($matches);
echo '</pre>';

That’s fantasic.

Thank you!