Solved

PHP Grep command help

Posted on 2013-05-22
4
211 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
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 109

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Learn by example how to specify CSS selectors for Selenium WebDriver test automation software.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

770 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