Solved

PHP Grep command help

Posted on 2013-05-22
4
205 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
  • 2
4 Comments
 
LVL 12

Expert Comment

by:sivagnanam chandrakanth
Comment Utility
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
Comment Utility
Thank you...will check that out
0
 

Author Comment

by:BHForum
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It is possible to boost certain documents at query time in Solr. Query time boosting can be a powerful resource for finding the most relevant and "best" content. Of course the more information you index, the more fields you will be able to use for y…
Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now