Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_match 1

Status
Not open for further replies.

JamesGMills

Programmer
Aug 15, 2005
157
GB
Hi people,

Can I use preg_match to get everything with an : after it from this into an array?

So for example, from this:
* Author: James Mills [james@koodoocreative.co.uk]
* Created: 18 April 2009

Get this:
$myInfo['Author'] (when printed = James Mills [james@koodoocreative.co.uk])
$myInfo['Created'] (when printed = 18 April 2009)

Thanks in advance,

James




------------------------
 
Hi

PHP:
[b]foreach[/b] [teal]([/teal][COLOR=darkgoldenrod]file[/color][teal]([/teal][green][i]'[URL unfurl="true"]http://pastie.org/681403.txt'[/URL][/i][/green][teal])[/teal] [b]as[/b] [navy]$line[/navy][teal])[/teal]
  [b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]preg_match[/color][teal]([/teal][green][i]'/(\w+):\s*(.+)/'[/i][/green][teal],[/teal][navy]$line[/navy][teal],[/teal][navy]$match[/navy][teal]))[/teal]
    [navy]$myInfo[/navy][teal][[/teal][navy]$match[/navy][teal][[/teal][purple]1[/purple][teal]]]=[/teal][navy]$match[/navy][teal][[/teal][purple]2[/purple][teal]];[/teal]

Feherke.
 
Sorry I have not been clear.

This is my code.

It looks through a directory and gets all the comments from those files.

From $comments[0] I then want to produce something like

$myInfo['Author'] (when printed = James Mills [james@koodoocreative.co.uk])
$myInfo['Created'] (when printed = 18 April 2009)

------------------------
 
Hi

By the way, if the text is not in an array, the [tt]preg_match_all()[/tt] function may be more suitable. Also if you wish to do more operations on the $match array.
PHP:
[b]if[/b] [teal]([/teal][COLOR=darkgoldenrod]preg_match_all[/color][teal]([/teal][green][i]'/(\w+):\s*(.+)/'[/i][/green][teal],[/teal][COLOR=darkgoldenrod]file_get_contents[/color][teal]([/teal][green][i]'[URL unfurl="true"]http://pastie.org/681403.txt'[/URL][/i][/green][teal]),[/teal][navy]$match[/navy][teal]))[/teal]
  [b]for[/b] [teal]([/teal][navy]$i[/navy][teal]=[/teal][purple]0[/purple][teal];[/teal][navy]$i[/navy][teal]<[/teal][COLOR=darkgoldenrod]count[/color][teal]([/teal][navy]$match[/navy][teal][[/teal][purple]1[/purple][teal]]);[/teal][navy]$i[/navy][teal]++)[/teal]
    [navy]$myInfo[/navy][teal][[/teal][navy]$match[/navy][teal][[/teal][purple]1[/purple][teal]][[/teal][navy]$i[/navy][teal]]]=[/teal][navy]$match[/navy][teal][[/teal][purple]2[/purple][teal]][[/teal][navy]$i[/navy][teal]];[/teal]


Feherke.
 
<?php

$handler = opendir("services/");
while ($file = readdir($handler))
{
if ($file != '.' && $file != '..')
{
$tokens = token_get_all(file_get_contents("services/" . $file));
$comments = array();
foreach($tokens as $token) {
if($token[0] == T_COMMENT || $token[0] == T_DOC_COMMENT) {
$comments[] = $token[1];
}
}

echo '<pre>';
print_r($comments[0]);
echo '</pre>';


if (preg_match_all('/(\w+):\s*(.+)/',$comments[0],$match))
for ($i=0;$i<count($match[1]);$i++)
$myInfo[$match[1][$i]]=$match[2][$i];

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

}



}
closedir($handler);

?>


gives me


------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top