الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

Php للبحث عن نص معين داخل عدة ملفات

بدأه amin1982 في 26 فبراير 2008 · 1 رد · 936 مشاهدة · في منتدى تطوير المواقع بـ PHP
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

لسلام عليكم

اريد ابسط برنامج php ليقوم بعمل بحث عن نص معين داخل عدة ملفات موجودة داخل عدة فولدرات يعني:

grep -R text *

و السلام عليكم

#2

السلام عليكم,

كان عندى شوية وقت فاضى فكتبت كلاس صغير, طبعا انا ملحقتش احط كل امكانيات grep فى الكلاس دة حاليا, هو دلوقتى كل الى بيعمله هو recursive search على المجلدات و الملفات الى انت بتحددهاله.

ملحوظة: الكود دة php 5 , و حتلاقى الـ DirectoryIterator فى SPL .

<?php

/**
 * this small class searches for strings in files and dirs
 * <code>
 *  $searcher = new SearchFile("/foo");
 *  var_dump($searcher->find('shreef');
 * </code>
 *
 *@author Ahmed Shreef < http://shreef.com >
 * last update feb 26, 2008 21:45 GMT 
 */
class SearchFile {

	/**
	 * the search path
	 *
	 *@var String
	 */
	protected $path = null;

	/**
	 * it's TRUE if the search path is directory
	 *
	 *@var Boolean
	 */
	protected $isDir = false;

	/**
	 * it's TRUE if we are allowed to search directories recursively
	 *
	 *@var Boolean
	 */
	protected $isRecursive = false;

	/**
	 * the needle to search for
	 *
	 *@var String
	 */
	protected $string = null;	

	/**
	 * the search results
	 *
	 *@var Array
	 */
	protected $result = null;

	/**
	 * Construct
	 *
	 *@param String	 the search path
	 *@param Boolean	is recursive search in dirs
	 */

	public function __construct($path, $isRecursive = FALSE){
		$this->setPath($path, $isRecursive);
	}

	/**
	 * sets the main search path and checks if it's Directory or File
	 *
	 *@param String	 the search path
	 *@param Boolean	is recursive search in dirs
	 *@return void
	 */
	public function setPath($path, $isRecursive = FALSE){

		$this->result = null;

		if( is_dir($path) ) {
			$this->isDir = TRUE;
		}elseif( is_file($path) ) {
			$this->isDir = FALSE;
		}else{
			throw Exception('path not found');
		}

		$this->path = $path;
		$this->isRecursive = (bool)$isRecursive;
	}

	/**
	 * starts the search for the passed string
	 *
	 *@param String	 the needle to search for
	 *@return Array
	 */
	public function find($string){

		$this->string = $string;

		if( $this->isDir ){
			$this->searchDir($this->path);
		}else{
			$this->searchFile($this->path);
		}	

		return $this->result;
	}

	/**
	 * reads file to search for the needle
	 *
	 *@param String	 the path of the file
	 *@return void
	 */
	protected function searchFile($path) {

		$lines = file($path);

		foreach($lines as $lnum=> $line) {

			if ( stristr($line, $this->string) ){

				$this->addResult($path, $lnum);

			}

		}
	}


	/**
	 * opens directory to list the files
	 *
	 *@param String	 the path of the directory
	 *@return void
	 */
	protected function searchDir($path) {

		$dir = new DirectoryIterator($path);

			  foreach ($dir as $file)
			  {

				if( !$file->isDot() ){
					if( !$file->isDir() ){
						$this->searchFile( $file->getPathname() );
					}elseif( $this->isRecursive ){
						$this->searchDir( $file->getPathname() );
					}
				}

			  }

	}


	/**
	 * adds a new record to the results var
	 *
	 *@param String	 the path of the file
	 *@param int		the line number
	 *@return void
	 */
	protected function addResult($path, $lineNum) {
		$this->result[$path][] = $lineNum;
	}


}

مثال استخدام

$searcher = new SearchFile('/media/hda2/server/c/lexical analyzer', true);

var_dump($searcher->find('void'));

Web Citizen

ShrefInLife.gif

__________

General blog : Shref In Life

مواضيع مشابهة