-4

I have to use php 7.3.x for a website, but I have a file that uses the strpos function. Is there a solution to use this function or is there a replacement in php 7 for this function?

this is my code

if(strpos($text, $data['word']) !== false) {
        if($data['data']['type'] == 'forward') {
            $ret = forwardMessage($chat_id,
                                  $data['data']['from_id'],
                                  false,
                                  $data['data']['message_id']);
        }

thank you.

Top30k
  • 1
  • 2
    `strpos` is PHP 4, PHP 5, PHP 7, PHP 8, so what is the specific problem? – Nigel Ren Jul 09 '21 at 10:37
  • 2
    [strpos](https://www.php.net/manual/en/function.strpos.php) is available in PHP7, does something not work? – brombeer Jul 09 '21 at 10:37
  • It's not clear why you think you can't use strpos in PHP7. Are you having a specific problem you'd like to tell us about? – ADyson Jul 09 '21 at 10:41
  • it doesnt work on php 7.3 or higher – Top30k Jul 09 '21 at 10:41
  • clearly i want use this function or a same function in php 7.3 or higher – Top30k Jul 09 '21 at 10:43
  • 1
    Please explain *it doesnt work*? – Nigel Ren Jul 09 '21 at 10:46
  • `it doesnt work` ...means what exactly? If you get an error or unexpected behaviour then - as mentioned already - you need to **tell us precisely what it is**. We cannot guess your specific problem, and as far as we are concerned, as per the official PHP documentation this function is supported in all current versions of PHP. So unless you clarify what you mean, we can't help you. – ADyson Jul 09 '21 at 10:56
  • 1
    I'm going to assume this is related to `$data` being null, which started raising a deprecation error in 7.3. See https://3v4l.org/TnTTk Check your variables before passing them to functions, and **please read the error messages that PHP is raising** – iainn Jul 09 '21 at 11:00
  • This is one of the functions of a telegram robot, the main job of the word search robot is to enter a post if they are the same. When I change the version from php 5.6 to php 7.3 or higher, it no longer works and the function fails. I was the solution that I read in the php documentation, this function does not work on this version and is deprecated., now my question is whether there is a solution for this or I can only use php 5.6 – Top30k Jul 09 '21 at 11:12
  • `I read in the php documentation, this function does not work on this version and is deprecated`...where did you read that exactly? It's not in the official documentation link you were given above. – ADyson Jul 09 '21 at 11:16
  • `it no longer works and the function fails`... "no longer works" is the same as "doesn't work", i.e. meaningless. so the function fails...fails **how** exactly? Specify the input data you provided to the function, the output you expected and the output you actually got. – ADyson Jul 09 '21 at 11:17
  • how can i gave you the full code and you test it? – Top30k Jul 09 '21 at 11:38
  • this document https://www.php.net/manual/en/function.strpos.php – Top30k Jul 09 '21 at 11:38
  • `how can i gave you the full code and you test it`...we don't need the _full_ code, we just need the things I already mentioned - the input data you provided to the function, the output you expected and the output you actually got. You can edit your question to include it (the "edit" button is at the bottom of it, just under the blue tags). See also the Stackoverflow guidance on how to create a [mre] – ADyson Jul 09 '21 at 11:48
  • https://www.php.net/manual/en/function.strpos.php does not mention deprecation of the function anywhere, I've no idea what you mean by that remark. The change log mentions that one specific behaviour has been deprecated at 7.3...but deprecation isn't the same as stopping working - that behaviour was only stopped entirely in PHP 8, so in 7.3 you should not have an issue (you might just get a warning / notice saying it will stop working in a future version). But we don't even know if your code would be affected by that change yet anyway, we'd need to see the input data to know that. – ADyson Jul 09 '21 at 11:49

1 Answers1

1

In php 7.3.0 there was a change with needle parameter passed to a function.

strpos(string $haystack, string $needle, int $offset = 0)

If needle is not a string, it is converted to an integer and applied as the ordinal value of a character. This behavior is deprecated as of PHP 7.3.0, and relying on it is highly discouraged. Depending on the intended behavior, the needle should either be explicitly cast to string, or an explicit call to chr() should be performed.