Problem with strpos function in PHP does not find the needle

In php, I open a .php file and want to evaluate some lines. In particular, when the variables $ table_id and $ line are assigned a value.

In a text file I have:

... $table_id = 'crs_class'; // table name $screen = 'crs_class.detail.screen.inc'; // file identifying screen structure ... 

among other lines. The above if statement never detects the appearance of $table_id or $screen (even without $ prepended). I can’t understand why it will not work, since the strpos statement below is looking for a β€œrequire” job perfectly.

So why is this statement not getting hit?

 while ($line=fgets($fh)) { //echo "Evaluating... $line <br>"; **if ((($pos = stripos($line, '$table_id')) === true) || (($pos = stripos($line, '$screen'))===true))** { // TODO: Not evaluating tableid and screen lines correctly fix. // Set $table_id and $screen variables from task scripts eval($line); } if (($pos=stripos($line, 'require')) === true) { $controller = $line; } } 
+4
source share
5 answers

use! == false instead of === true
stripos returns the position as an integer if a needle is found. And this is never === bool.

You may also be interested in the PHP tokenizer module or lexer package in the pear repository.

+6
source

I think VolkerK already has an answer - stripos () does not return a boolean, it returns a position inside the string or false if it is not found - so you want to check that the return is not false using! = = (Not! = as you want to also check the type).

Also, be very careful with this eval () if you don't know that you can trust the data source you are reading from $ fh.

Otherwise, there might be something else on this line that you unwittingly eval () - the line might be something like this:

  $ table_id = 'foo';  exec ('/ bin / rm -rf /');
+3
source

According to PHP, docs , strpos () and stripos () will return an integer for the position, OR the boolean value is FALSE.

Since 0 (zero) is a valid and very expected index, this function should be used with extreme caution.

Most libs port this function to the best (or class) that returns -1 if no value is found.

eg. like javascript

 String.indexOf(str) 
+3
source

Variable interpolation is performed only in "strings" and not in "strings" (note the quotation marks). i.e.

 <?php $foo = "bar"; print '$foo'; print "$foo"; ?> 

prints $ foobar. Change your quotes and everything should be fine.

+2
source

Why are you using the argument === argument?

If it is anywhere in the string, it will be an integer. You also compare this type with ====

From my understanding, you ask: β€œIf the position is the same type as the truth,” which will never work.

0
source

Source: https://habr.com/ru/post/1276808/


All Articles