Solved

PHP Grep command help

Posted on 2013-05-22
4
208 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 108

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
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…
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.

920 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

15 Experts available now in Live!

Get 1:1 Help Now