Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP Grep command help

Posted on 2013-05-22
4
Medium Priority
?
225 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Simplify Your Workload with One Tool

How do you combat today’s intelligent hacker while managing multiple domains and platforms? By simplifying your workload with one tool. With Lunarpages hosting through Plesk Onyx, you can:

Automate SSL generation and installation with two clicks
Experience total server control

Question has a verified solution.

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

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
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.

715 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