i have a very simple script that reads out a txt file, puts the content in an array.
Which does perfectly, i can do print_r($array); and it outputs all the data.
My script:
<?php
$file = 'countries.txt';
$countries_output = file_get_contents($file);
$countries_pieces = explode("\n", $countries_output);
if (in_array("Sweden", $countries_pieces)) {
echo "Sweden was found";
}
else
{
echo'NOT FOUND';
}
print_r($countries_pieces);
?>
I don't understand why it doesn't find the value 'Sweden' in my array, when it clearly is in there.
This is the output: https://pastebin.com/z9rC9Qvk
I also print_r the array, so you can see that 'Sweden' is indeed in the array.
Hope someone can help :)
There is most likely new line characters that you're not taking into account. The following is a cleaner solution using file() and should work for you:
$file = 'countries.txt';
$countries_pieces = file($file, FILE_IGNORE_NEW_LINES);
if (in_array("Sweden", $countries_pieces)) {
echo "Sweden was found";
} else {
echo'NOT FOUND';
}
If there are still some issues, a common normalization is to trim() values to remove some left-overs:
$countries_pieces = array_map('trim', $countries_pieces);
But this must not cure all issues.
Is countries.txt from a Windows machine? If so, splitting on '\n' won't work very well since there's also a '\r' for every line.
Your print_r output would seem to indicate that since there seems to be an extra newline between every line of output.
Related
Although a $_FILES array is not shown, this is intended for file upload, I have had problems with file uploading so I was not able to get the [type] part of the $_FILES array yet... but this is just a simple problem of why isn't this function working...
<?php
function getExtension() {
// global $testFile;
$extension = "./mp3/"; // also tried simply mp3 without the forwards slashes
$testFile = "song.mp3";
if(preg_match($extension, $testFile)) {
echo "match found";
}else {
echo "no match found";
}
}
getExtension();
?>
Do this:
$extension = "/mp3$/";
or
$extension = "/\.mp3$/";
ONLINE EXAMPLE
Reason being, you need the delimiters to begin and end your expression with (those are the slashes, though the can be any character) and can't have anything ahead of or behind them. The "$" will mean, find the string between the delimiters at the end of the string.
You can keep the period by escaping it (or else it will mean any character once, meaning testmp3 would be a match).
But really the best answer is as suggested in the comments - php's pathinfo() - since you are parsing a filename. Though in certain cases, you may want to do other tests for security, like check the mimetype.
If you want to keep the period, make sure you escape it.
$pattern = "/\.mp3$/";
There is a mistake in your Regular Expression :
"./mp3/" => "/\.mp3$/"
Result :
<?php
function getExtension() {
// global $testFile;
$extension = "/\.mp3$/"; // also tried simply mp3 without the forwards slashes
$testFile = "song.mp3";
if(preg_match($extension, $testFile)) {
echo "match found";
}else {
echo "no match found";
}
}
getExtension();
?>
ONLINE EXAMPLE
I want PHP read my article text file like this.
sample text file :
OMG! Where is my right hand.
I try to find my right hand but I can't see it.
please tell me how to find it.
Now I have this function code
function getContent($file_path,$path=''){
$file_path = $file_path;
if(file_exists('./'.$file_path)){
$f_read = fopen($path.$file_path,'r');
$rs = "";
while (!feof($f_read)) {
$rs .= fread($f_read, 8192);
}
fclose($f_read);
}
else{
echo $rs = "Not Connect File !";
}
return($rs);
}
after use that code :
OMG! Where is my right hand. I try to find my right hand but I can't see it. please tell me how to find it.
I want to use PHP function read first line to string1 and after first line is string2 like this
$string1 = "OMG! Where is my right hand."
$string2 = "I try to find my right hand but I can't see it.
please tell me how to find it."
Help me please :)
You can use below code for splitting your paragraph into string and also assign the character (like period, comma, etc.) from where do you want the paragraph to split.
preg_split('/[.?!]/',$mystring);
You may refer this link for more information: Explode a paragraph into sentences in PHP
or
http://php.net/manual/en/function.explode.php
You can use $string=#file_get_contents($path_name); $string=#explode("\n",$string); //for get each line.
In order to to read the first line into $string1 and the rest of the file into $string2 ...
Read the first line, as you do above, then call file_get_contents to get the rest.
Use the offset parameter to tell file_get_contents() to start reading after the end of the first line (pass the string length of the first line).
try using explode() function of PHP
function getContent($file_path,$path=''){
$file_path = $file_path;
if(file_exists('./'.$file_path)){
$f_read = fopen($path.$file_path,'r');
$rs = "";
while (!feof($f_read)) {
$rs .= fread($f_read, 8192);
$demo=explode('.', $rs);
}
fclose($f_read);
}
else{
echo $rs = "Not Connect File !";
}
return($demo);//result wiil be stored in $demo array like $demo[0], $demo[1]
}
My code that reads an XML file is as follows:
<?php
if ( $dance['block'] == $dance['user']['ip'] ) : ?>
<?php e(o("N/A")) ?>
Right now, that if() statement is reading the entire XML file as one string. So next I used:
$array6 = explode(",", $this->block);
$result['block'] = (string) $array6;
This was supposed to explode each $dance['user']['ip'], but I'm not sure why it's not working.
Any ideas, fixes, or tips?
Casting an array to a string will unhelpfully give you the string "Array" as well as throw a nifty warning. I'm not quite sure what you are trying to accomplish there.
For your test to see if a user's ip is in the block list you should do the following:
$blockList = explode(",", $blockedIpString);
if (in_array($dance['user']['ip'], $blockList)){
// user is blocked
} else {
// user is not blocked
}
I need some help with my PHP script.
So, here is the deal.
I have a preg_match which checks for the following text in a .txt file that changes every 30minutes or so: LYBE_TWR,LYBE_APP,LYBA_CTR,LYPG_TWR,LYPG_APP,LYTV_TWR,LYNI_APP)
It runs perfectly and gets the above strings if they are present.
But I need to do even further and check which of the seven combinations have been found because not all are present at all times.
Example:
The current text file contains LYBE_TWR, LYBE_APP, LYPG_TWR. The preg_match does its thing and I can echo the 3 values but I need this.
LYBE_TWR : PRESENT/NOT PRESENT
LYBE_APP: PRESENT/NOT PRESENT
LYBA_CTR PRESENT/NOT PRESENT
LYPG_TWR PRESENT/NOT RESENT
etc.
So if it is found in the text file it echos present, if not it echoes not present.
The correct results would be:
LYBE_TWR : PRESENT
LYBE_APP: PRESENT
LYBA_CTR NOT PRESENT
LYPG_TWR PRESENT
If I do for example if ($string == "LYBE_TWR") { echo 'present'; } else { echo 'not present'} it will echo the correct value for the LYBE_TWR but it will say not present for the later as they are not actually the one I if-ed for.
I hope you understand as I myself am not sure anymore (rofl)
edit: here is the current code..bare in mind it is still WIP so not finished and there will be some errors http://pastebin.com/z1r4A78E
Thanks.
This is the code working on the assumption that you're interested if the string appeared in your file, not in the line. I split the strings into prefixes and suffixes, and I'm ignoring any numerical values. I didn't copy all your code, for readability's sake
$prefixes = array("LYBA", "LYBE", "LYPG", "LYNI", "LYTV");
$suffixes = array("TWR", "APP", "CTR");
foreach($prefixes as $prefix)
{
foreach($suffixes as $suffix)
{
$results[$prefix."_".$suffix] = 0;
}
}
if(preg_match('/^('.implode("|",$prefixes).'|)_[A-Z0-9]*_*('.implode("|",$suffixes).')/', $line, $matches))
{
(... your code ...)
$match_string = $matches[1]."_".$matches[2];
$results[$match_string]++;
}
foreach($results as $key => $value)
{
echo $key;
if($value > 0)
{
echo " PRESENT";
}
else
{
echo " NOT PRESENT";
}
echo "<br/>";
}
I am having one hell of a time coming up with a decent way make this if statement search a file for these codes. I set up the text file to read from as such:
myfile.txt
r)
0Y7
1a6
q.
#g
#(
#a
!P
T[
V}
0,
Here is a brief of what I got going.
$subject = file_get_contents(fvManager_Path . 'myfile.txt');
if ( preg_match('/^[a-zA-Z0-9,]+$/',$result['fmbushels_itemCode'], $subject) ) {
Basically I am trying to search the text file line by line to see if the whole string exists. They are case sensitive as well.
$result['fmbushels_itemCode'] is from a sql query and always returns a code like the above in the text.
I'd appreciate any help on this. If someone knows a better way of doing this or a different command, I'd be willing to give that a shot as well :)
edit:
private function _fvShareBushels() {
$subject = file_get_contents(fvManager_Path . 'myfile.txt');
if (count($vShareArray) > 0) {
$vCntMoves = count($vShareArray);
for ($vI = 0;$vI < $vRunMainLoop;$vI++) {
sell $result['fmbushels_itemCode']);
}
}
}
This is a snippet of a big code. I had to rip most out because of post limitation. The area I could be working with is:
if (count($vShareArray) > 0) {
If I could make this something like:
if (count($vShareArray) > 0 && $result['fmbushels_itemCode'] **is not in** $subject) {
If you want to do line by line, use the file() function.
$f = file(fvManager_Path . 'myfile.txt');
foreach($f AS $line){
// $line is current line at file
}
I'm not to sure if you understand completely how preg_match works. The first parameter is the regular expression pattern, the second is what you want to match the pattern to, and the third is an array of matches. So for every valid pattern matched in the second parameter a new index on the array is created.
I'm not 100% on what you're trying to accomplish. Are you trying to see if the $result['fmbushels_itemCode'] exists in the file?
If the above is the correct case you simply just need to do something like:
$f = file('myfile.txt');
array_map('trim', $f);
if(in_array($result['fmbushels_itemCode'], $f)){
// success
}