LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-01-2011, 09:24 AM   #1
gcclinux
Member
 
Registered: Oct 2004
Location: London, UK
Distribution: Suse 12.1
Posts: 79

Rep: Reputation: 15
Question php search for string and print line


I hope someone can help me, I am trying to do the following in php.

$file = $source;
$search = "pattern";

while reading the $file line by line search for pattern;

if pattern found
echo "whole the line with the pattern"


I hope that make sense I really appreciate the help.

Many Thanks
 
Old 08-01-2011, 10:10 AM   #2
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Try the code below:

Regards,

<?php
if ($file_handle = fopen("$file", "r"))
{
while ($file_line = fgets($file_handle))
{
if (preg_match("/$search/", $file_line))
{
print "$file_line";
}
/*
else
{
print "NO $search: $file_line";
}
*/
}
fclose($file_handle);
}
/*
else
{
print "FAILED to open $file";
}
*/
?>
 
1 members found this post helpful.
Old 08-01-2011, 10:44 AM   #3
gcclinux
Member
 
Registered: Oct 2004
Location: London, UK
Distribution: Suse 12.1
Posts: 79

Original Poster
Rep: Reputation: 15
Hi tshikose,

You post is brilliant, very and quick and perfect and you even added commented out if failed to open file, that was great, thank you so much, it works.

so I am going to be cheacky and and as one more question regarding this, can I use a array (multiple patterns, that would print out 1 per line.

search = array('pattern_1', 'pattern_2', 'pattern_3');

then intead of

if (preg_match("/$search/", $file_line))

do something like

foreach (preg_match("/$search/", $file_line))
{
print "$file_line <BR>";
}




I've tried that but did not work


Anyway I would like to say thanks for the help you have already provided.
 
Old 08-01-2011, 11:07 AM   #4
gcclinux
Member
 
Registered: Oct 2004
Location: London, UK
Distribution: Suse 12.1
Posts: 79

Original Poster
Rep: Reputation: 15
Hi I just wanted to update that I've cheated and although it is not the most economical way of running the script it works.

<?php
include 'config.php';
$file = $source;

$search_0 = 'pattern_0';
$search_1 = 'pattern_1';
$search_2 = 'pattern_2';
$search_3 = 'pattern_3';

if ($file_handle = fopen("$file", "r"))
{
while ($file_line = fgets($file_handle))
{
if (preg_match("/$search_0/", $file_line))
{ print "$file_line "; }
if (preg_match("/$search_1/", $file_line))
{ print "$file_line "; }
if (preg_match("/$search_2/", $file_line))
{ print "$file_line "; }
if (preg_match("/$search_3/", $file_line))
{ print "$file_line "; }

}
fclose($file_handle <BR>);
}
?>
 
Old 08-02-2011, 02:32 AM   #5
tshikose
Member
 
Registered: Apr 2010
Location: Kinshasa, Democratic Republic of Congo
Distribution: RHEL, Fedora, CentOS
Posts: 525

Rep: Reputation: 95
Hi,

No matter, as it works.
Mark the thrad as solved.

Regards,
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
vim 'n' lynx: command line option to search for string Samsara Linux - Software 6 01-02-2013 06:36 PM
[SOLVED] search a string and print column vikas027 Programming 6 03-14-2010 09:20 AM
search string in text and print string wolfipa Linux - Software 4 09-17-2009 08:50 AM
search and replace with multi line string kambrish Programming 5 04-28-2008 06:02 AM
Parsing a string line-by-line in PHP enigma_0Z Programming 3 04-21-2006 08:07 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration