Searching an array - regular expression

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Gargomel7
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2003 8:44 am

Searching an array - regular expression

Post by Gargomel7 »

php 4.2.2

i pulled a file into an array. each line of the file is pretty much a sentence.

i want to search each piece of the array with a regular expression....

so if the array is array("lisa walks the dog", "dave walks the dog", "jeff walks the dog") i want to search for "lisa" and have it return the piece of the array that its in.

any help would be greatly appreciated.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Unfortunately the server move ate my first answer to this question but I'll try again, have a look at foreach and stristr(). My untested code is as follows:

Code: Select all

$lines = file('myfile.txt');

foreach ($lines as $line_num => $line) {
    if (stristr($line, 'lisa')) {
        $matches[$line_num] = $line;
    }
}

echo '<pre>';
print_r($matches);
echo '</pre>';
Mac
Gargomel7
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2003 8:44 am

Post by Gargomel7 »

that worked!!.... here's what the full array looks like....

[0] => MICHAEL 2043 09:00 27 0 2 0 1 04:42 02:38 00:00 00:31 00:00 52.6 14.3 8.8 23.5 0.0 0.2 0.0 0.7

[1] => JEFF 2099 09:04 29 0 13 1 0 05:12 01:07 00:00 01:07 07:08 47.7 18.5 1.6 27.8 0.0 2.7 1.3 1.7

[2] => LISA 2619 09:08 15 0 3 2 2 07:02 00:02 00:00 02:48 00:26 61.1 17.6 0.0 19.2 0.0 1.5 0.2 0.5

[3] => TINA 2618 05:57 12 0 1 1 1 06:42 05:01 00:00 01:12 00:31 53.5 5.3 16.9 22.6 0.0 0.3 0.1 1.4

[4] => HEATHER 2638 08:51 0 0 0 0 0 00:00 00:00 00:00 00:00 00:00 0.0 100.0 0.0 0.0 0.0 0.0 0.0 0.0

[5] => JOHNG 2506 09:02 25 0 0 1 0 06:20 03:30 00:00 00:00 00:18 30.3 15.7 23.9 29.2 0.0 0.0 0.1


can i pull multiple array pieces? say i want to find lisa, tina, heather?
Gargomel7
Forum Newbie
Posts: 4
Joined: Tue Sep 30, 2003 8:44 am

Post by Gargomel7 »

Here's the code....and i can search the entire file array and get the ones i want in the final array.....

how can i parse this info and insert it into a mysql db so that each line is split into fields in the db?

Code: Select all

$pattern = array("2411", 
				 "2061",
				 "2538", 
				 "2232", 
				 "2620",
				 "2345",
				 "2021",
				 "2430",
				 "2540",
				 "2210");

$pattern_count = count($pattern);

$i = 0;
while ($i<$pattern_count) &#123;
$acd_list = file("/var/www/html/cs/acdtest.txt");
$search = $pattern&#1111;$i];

foreach ($acd_list as $line_num => $acd_list) &#123; 
    if (stristr($acd_list, $search)) &#123; 
        $matches&#1111;$line_num] = $acd_list; 
    &#125; 
&#125; 

++$i;
&#125;

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