Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,879 questions

51,805 answers

573 users

How to search a word within text file and print the whole line in PHP

1 Answer

0 votes
$file = 'data.txt';
$searchword = 'php';

$content = file_get_contents($file);

$pattern = preg_quote($searchword, '/');

$pattern = "/(?i)^.*$pattern.*\$/m";
if (preg_match_all($pattern, $content, $matches)) {
	echo "<pre>";
   	print_r($matches[0]);
	echo "</pre>";
}
else {
   echo "Not found";
}


/*

data.txt
--------

PHP is a general-purpose scripting language
that is especially suited to web development.
It was originally created by Danish-Canadian programmer
Rasmus Lerdorf in 1994
the PHP reference implementation is now
produced by The PHP Group

*/


/*
run:

Array
(
    [0] => PHP is a general-purpose scripting language
    [1] => the PHP reference implementation is now
    [2] => produced by The PHP Group
)

*/

 



answered Aug 22, 2020 by avibootz

Related questions

1 answer 172 views
1 answer 191 views
1 answer 151 views
1 answer 196 views
1 answer 156 views
...