PHP Check If File Contains a String

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.

fwrite if string doesn't exist in file

This is hopefully easier to understand:

$file = 'assets/sitemap/1.txt';
$url = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI]."\n";

$text = file_get_contents($file);

if(strpos($text, $url) === false) {
file_put_contents($file, $url, FILE_APPEND);
}
  • Read the file contents into a string $text using file_get_contents()
  • Check if $url is in the string $text using strpos()
  • If $url is not in the string $text, append the $url to the file using file_put_contents()

To count the total lines, you can start using file() to load the file lines into an array. Then check if the $url is in the array using in_array():

$lines = file($file);
$count = count($lines); // count the lines

if(!in_array($url, $text)) {
file_put_contents($file, $url, FILE_APPEND);
$count++; // if added, add 1 to count
}

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);

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";
}

Check if first line of file contains string

Issue solved by looking exact places where I need to look :)

if (strpos($data[0],'Value') !== false)


Related Topics



Leave a reply



Submit