-1

I have this code:

if ( strpos( $_GET[ 'y' ], array( '/', '.', '\\', '%', '#', ';' ) ) === false

it worked fine on php5 but now on php 8 I get this error:

 strpos(): Argument #2 ($needle) must be of type string, array given

Do I have to get rid of that line altogether or is there any alternative fix out there?

halfer
  • 19,824
  • 17
  • 99
  • 186
Cain Nuke
  • 2,843
  • 5
  • 42
  • 65

1 Answers1

1

If you read the documentation https://www.php.net/manual/en/function.strpos.php you can see that both params need to be strings (as the error actually tells you).

If you wanna search for multiple stings either use a regular expression with

preg_match()

Or search online for strpos with multiple needles. It's even answered here in stack overflow strpos() with multiple needles? or here Using 2 or more needles when using strpos

Hope this helps ;)

Ext
  • 657
  • 1
  • 8
  • 13
  • I tried preg_match like this: `preg_match('/\/|\.|\\\\|\%|\#|\;/i', $_GET[ 'y' ])` I got no error so far but Im not sure if this is what you mean. – Cain Nuke Apr 21 '23 at 18:23
  • I think you need something like this: if ( preg_match('/[\/\.\\%#;]/i', $_GET['y']) ) { echo 'Yes'; } else { echo 'No'; } – Ext Apr 21 '23 at 18:32
  • @CainNuke `/\/|\.|\\\\|\%|\#|\;/` is the same as `![/.\\\\%#;]!`. No need for `i` modifier, and change the delimiter so you don't need to escape it. – user3783243 Apr 21 '23 at 18:51
  • Are you sure? I cant make that one work here https://regexr.com/ – Cain Nuke Apr 21 '23 at 18:57
  • @CainNuke https://regex101.com/r/zjihoc/1 there's a regex demo. Not sure what `$_GET[ 'y' ]` has so can't test it – user3783243 Apr 21 '23 at 20:21