Search
 
SCRIPT & CODE EXAMPLE
 

PHP

searching inside a file using php

<?php
$file = 'somefile.txt';
$searchfor = 'name';

// the following line prevents the browser from parsing this as HTML.
header('Content-Type: text/plain');

// get the file contents, assuming the file to be readable (and exist)
$contents = file_get_contents($file);
// escape special characters in the query
$pattern = preg_quote($searchfor, '/');
// finalise the regular expression, matching the whole line
$pattern = "/^.*$pattern.*$/m";
// search, and store all matching occurences in $matches
if(preg_match_all($pattern, $contents, $matches)){
   echo "Found matches:
";
   echo implode("
", $matches[0]);
}
else{
   echo "No matches found";
}
Comment

PREVIOUS NEXT
Code Example
Php :: compile custom/plain css with mix in laravel 
Php :: woocommerce bulk product delete 
Php :: laravel timestamp 
Php :: create form request laravel 
Php :: enum in laravel migration 
Php :: symfony server start port 
Php :: multidimensional array item remove php 
Php :: php creazione numero random 
Php :: php number format 
Php :: php get current month first date 
Php :: remove notices php 
Php :: check current pages is a child page wordpress 
Php :: how validate array in laravel in request 
Php :: wordpress is home page 
Php :: php requuire once 
Php :: wordpress programmatically logout 
Php :: laravel include with variable 
Php :: php fpm config file location 
Php :: how to display image in wordpress 
Php :: php list directories 
Php :: laravel log could not be opened fix 
Php :: get country from ip 
Php :: add to json object php 
Php :: wordpress add_submenu_page 
Php :: upload file in php 
Php :: base url laravel 
Php :: array_fill php 
Php :: Sending Data over another website via PHP 
Php :: convert timestamp to date php 
Php :: laravel validation array unique values 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =