Jump to content

I posted  recently some of my troubles figuring out PHP, but I think I have a more specific question that may be more easily answered. I've come to realize that my code, which takes a variable from a file and checks if its in a string using the strpos() function doesn't work because the variable is taken from a file. If the variable is just assigned normally it works fine, but it seems that strpos() will always return false when checking for a variable taken from a file. Does anyone know why this is or how to solve this problem? Thanks

Link to comment
https://linustechtips.com/topic/619639-php-strpos-from-a-variable-taken-from-a-file/
Share on other sites

Link to post
Share on other sites

9 minutes ago, alexboz said:

Could you post a code sample/example?

$file = "status.txt";
$lines = file($file, FILE_IGNORE_NEW_LINES);

$servername = $lines[1];

$output = "[hello] => world
[servername] => s9
[linus] => techtips";

var_dump(strpos($output, "[servername] => $servername"));

The var_dump returns "bool(false)"

 

Edit: It should be returning the position of the string "s9" as the second line in the file is simply "s9"

Link to post
Share on other sites

4 minutes ago, SSL said:

I don't know what you're trying to do here, but it seems like an incredibly wack way to do it.

 

Why not check the variable pulled from the file with var_dump?

I'm trying to figure out if the variable $output contains the variable $servername, but it doesn't seem to work because $servername is taken from the file. If the code were simply:

$servername = "s9";

It would then work. I need to figure out how to make it work with the file

Link to post
Share on other sites

1 minute ago, Lunar Evolution said:

I'm trying to figure out if the variable $output contains the variable $servername, but it doesn't seem to work because $servername is taken from the file. If the code were simply:


$servername = "s9";

It would then work. I need to figure out how to make it work with the file

 

So call var_dump on $servername or use a debugger. You didn't bother to post the contents of your file, so it's kind of hard to help you beyond the obvious suggestion.

Link to post
Share on other sites

2 minutes ago, alexboz said:

Try dumping the value of $servername. What are the contents of the file?

Dumping $servername returns the string "s9". The contents of the file are:

partial outage
s9
major outage
API (example)
partial outage
Shared Hosting

HOWEVER! I would like to note that once I copied the file (which looks totally normal in text edit), after pasting into the code snippet tool here, it actually had tons of red dots between "partial outage" and "s9". These red dots disappear when clicking out of the snippet. Thoughts?

Link to post
Share on other sites

3 minutes ago, SSL said:

 

So call var_dump on $servername or use a debugger. You didn't bother to post the contents of your file, so it's kind of hard to help you beyond the obvious suggestion.

I didn't think the contents were necessary, was hoping that this was a known issue that I didn't understand (as I am very new to PHP) as it works without the use of variables taken from files but not with them

Link to post
Share on other sites

Then there must be more than just s9 on the second line, probably hidden characters. There is probably a better way to accomplish what you are trying to do here. Could you give us some more background of what this is for and maybe we can work out a better solution?

Link to post
Share on other sites

10 minutes ago, SSL said:

Oh, I know what it is.

 

Please re-read the documentation for the file function: http://php.net/manual/en/function.file.php

 

Particularly the return value.

I read over it, not exactly sure what you wanted me to see.. from what I understand I have correctly removed the new lines an accessed the array using the proper syntax

Link to post
Share on other sites

9 minutes ago, SSL said:

Oh, I know what it is.

 

Please re-read the documentation for the file function: http://php.net/manual/en/function.file.php

 

Particularly the return value.

If you were talking about the line endings, they're using FILE_IGNORE_NEW_LINES, which returns the array without the line endings. I tested this with and without FILE_IGNORE_NEW_LINES, then used if to check line 2 for "s9".

Link to post
Share on other sites

1 minute ago, alexboz said:

If you were talking about the line endings, they're using FILE_IGNORE_NEW_LINES, which returns the array without the line endings. I tested this with and without FILE_IGNORE_NEW_LINES, then used if to check line 2 for "s9".

 

It must be the mac line endings. If var_dump supposedly prints out s9 in both cases, the only other possibility is that there are non-printing characters present in one case and not the other.

Link to post
Share on other sites

9 minutes ago, SSL said:

 

It must be the mac line endings. If var_dump supposedly prints out s9 in both cases, the only other possibility is that there are non-printing characters present in one case and not the other.

I think I may have (kind of?) figured it out. The file, which originated using a shell script containing a command line argument and a python program to parse the output into a file, contained some characters (maybe around 1300?) that were not showing up in any text editors. After manually changing the file, I was able to find "s9" where it was located, and upon running the scrip again it produced a file that looked the same as the old file, but had no issues with invisible characters. I'm not sure what the root of the issue was really, but I think its fixed for now. Thanks for the help everyone, apologies that it wasn't exactly what I thought it was

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×