Solved

PHP Grep command help

Posted on 2013-05-22
4
220 Views
Last Modified: 2013-10-21
Just beginning with PHP. I am trying to use a PHP page to run something similar to the following Linux command:

grep "text string" filename

Open in new window


and to have it output to a web page just as the output of the grep command. Basically listing every line containing the search string.
0
Comment
Question by:BHForum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
ID: 39189886
As a beginner go through these string commands first http://www.w3schools.com/php/php_ref_string.asp.

There are many functions to find the occurrence of a string.. strstr,strchr,etc... the way for doing it would differ  based on the exact requirement
0
 

Author Comment

by:BHForum
ID: 39194869
Thank you...will check that out
0
 

Author Comment

by:BHForum
ID: 39194904
Looking through these string commands, none seem to list all lines containing the string. They will find first, last, count, position, etc., but do not output showing all lines containing the string. For instance, if I had the following text file:

Line of text containing words
This is the second line of text
This line also contains words
This is the fourth line of text

Open in new window


and I search for the string "words" the result should be:

Line of text containing words
This line also contains words

Open in new window

0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39195035
Good places to start learning PHP:
http://php.net/tut.php
http://www.sitepoint.com/books/phpmysql5/
http://www.amazon.com/PHP-MySQL-Web-Development-Edition/dp/0672329166/

You can read a file into an array with the PHP file() function, simulated in this script.

Please see: http://www.laprbass.com/RAY_temp_bhforum.php

<?php // RAY_temp_bhforum.php
error_reporting(E_ALL);
echo '<pre>';

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28136625.html

// TEST DATA AS IF IT WERE READ FROM A FILE WITH FILE() FUNCTION
$arr = array
( 'Line of text containing words'
, 'This is the second line of text'
, 'This line also contains words'
, 'This is the fourth line of text'
)
;

// SEARCH TARGET
$tgt = 'words';

// PROCESS EACH OF THE TEST DATA ELEMENTS
foreach ($arr as $str)
{
   if (FALSE !== strpos($str, $tgt)) echo PHP_EOL . "FOUND <b>$tgt</b> IN $str";
}

Open in new window

HTH, ~Ray
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question