How can I tell if a file is text using PHP?
Try the finfo_file()
function.
Here's a blog describing its usage: Smart File Type Detection Using PHP
how to check if date and string exist in text file
The sequence date("F j, g").' '.$_SESSION['name']
cannot exists, because there are some characters between the date and the user name.
You could check use a simple regular expression like "January 18, 9:[0-9]{2} AM"
$date = date('F j, g:[0-9]{2} A');
$match = preg_match('~' . $date . '~', $text_message);
if (strpos($text_message, $_SESSION['name']) === false && !$match) {
echo "match not found";
// file_put_contents()
}
else {
echo "match found";
}
PHP check if file contains a string
Much simpler:
<?php
if( strpos(file_get_contents("./uuids.txt"),$_GET['id']) !== false) {
// do stuff
}
?>
In response to comments on memory usage:
<?php
if( exec('grep '.escapeshellarg($_GET['id']).' ./uuids.txt')) {
// do stuff
}
?>
How to check if a txt file contain a word
You should search within a text content, not within file name.
Use file_get_contents
function:
$a = 'grzn.txt';
if (strpos(file_get_contents($a), 'word') !== false) echo 'found';
http://php.net/manual/en/function.file-get-contents.php
Seeing if a text file contains a word in php
You're searching for the word list as a whole in the string. Instead, you have to search for every word in the word list separately. For example, str_word_count
can be used to split the string into words.
<?php
$productFile = file_get_contents('products.txt');
$products = str_word_count($productFile, 1);
$status = 'i love watching tv on my brand new apple mac';
$found = false;
foreach ($products as $product)
{
if (strpos($status,$product) !== false) {
$found = true;
break;
}
}
if ($found) {
echo 'the status contains a product';
}
else {
echo 'The status doesnt contain a product';
}
?>
You may also want to consider stripos
instead of strpos
for case-insensitive comparison.
PHP: How to check if form data is the same as in a text file?
First you have to check the file by reading and string search, if the string is not found then perform action. please refer below example.
<?php
$productFile = file_get_contents('products.txt');
$products = str_word_count($productFile, 1);
$status = 'i love watching tv on my brand new apple mac';
$found = false;
foreach ($products as $product)
{
if (strpos($status,$product) !== false) {
$found = true;
break;
}
}
if ($found) {
echo 'the status contains a product';
}
else {
echo 'The status doesnt contain a product';
}
?>
PHP How to check if a file with a list of string contain a full word
You need to encapsulate separator (coma-point, @, # ,...) to your names in data source.
After add them in your test, remove ucfirst function and convert to upper case (only for test).
With this way, character case of first names in your text file 'firstname.txt' don't matter
@Jack@
@mArioN@
@pAuL@
// Define separator
$separator = '@';
// Pre-calculation of test first name
$firstNameTest = $separator . strtoupper($first_name) . $separator;
$handle = fopen('firstname.txt', 'r');
$valid = 0; // init as false
while (($buffer = fgets($handle)) !== false) {
// Test upper case buffer versus pre-calculated first name
if (strpos(strtoupper($buffer), $firstNameTest) !== false) {
$valid = 1;
}
}
fclose($handle);
Update (without altering source file)
If source file cannot be altered, an other way could be to add a carriage return at the end of your $first_name
.
// Pre-calculation of test first name
$firstNameTest = strtoupper($first_name) . PHP_EOL;
$handle = fopen('firstname.txt', 'r');
$valid = 0; // init as false
while (($buffer = fgets($handle)) !== false) {
// Test upper case buffer versus pre-calculated first name
if (strpos(strtoupper($buffer), $firstNameTest) !== false) {
$valid = 1;
}
}
fclose($handle);
Related Topics
PHP Passing C Struct Data Throught Socket Programming
The Post Method Is Not Supported for This Route. Supported Methods: Get, Head. Laravel
Jquery Ajax Settimeout After Form Succes Not Redirecting
Laravel Socialite: Invalidstateexception
How to Check If Time Is Between Two Times in PHP
PHP Get Url of Redirect from Source Url
Why Are "Echo" Short Tags Permanently Enabled as of PHP 5.4
Convert Invalid JSON into Valid JSON
Csrf (Cross-Site Request Forgery) Attack Example and Prevention in PHP
Getting ’ Instead of an Apostrophe(') in PHP
Why We Need Curlopt_Ssl_Verifypeer in Windows
Trying to Access Array Offset on Value of Type Bool in PHP 7.4
PHP Array Behaving Strangely with Key Value 07 & 08
Any Decent PHP Parser Written in PHP
Call Laravel Controller via Command Line
Connecting to Remote MySQL Server Using PHP
File_Get_Contents Receive Cookies
How to Use Objects from Other Namespaces and How to Import Namespaces in PHP