Why so weird looking substr?

$findstr = ';a:29:{s:4:"name";s:12:"the Loggia";s:4:"type";s:8:"checkbox";s:6:"slider";s:0:"";s:11:"slider_st';
$posnameind=strpos($findstr ,'name');
$posnamevalstart=strpos($findstr ,"\"", $posnameind+6);
$posnamevalend=strpos($findstr ,"\"", $posnamevalstart); 
$namevalue=substr($findstr ,$posnamevalstart+1,$posnamevalend);
print_r($namevalue.'<br/>');

Can somebody explain to me why the result of this code is equal to "the Loggia";s:4:"type".
Don't know how to get out of this sarizonana JSON values.
March 23rd 20 at 19:46
4 answers
March 23rd 20 at 19:48
Solution
First in this row is Cyrillic, so you need to use functions to work with multibyte strings.

Secondly, the variables $posnamevalstart and $posnamevalend you have the same values. Once you are looking for in both variables to the same value, it is necessary to shift the starting point:
$posnamevalend = mb_strpos($findstr, "\"", $posnamevalstart + 1);

Thirdly the third parameter in the function substr() is passed the LENGTH of the RETURNED STRING. So you need to write this string as follows:
$namevalue = mb_substr($findstr, ($posnamevalstart + 1), ($posnamevalend - $posnamevalstart - 1));

And most importantly - why are you doing this? If you just understand how to work with strpos() and substr() then okay. And so there you have the usual serialized string (the only reason you cropped) that is in an array function unserialize()
thanks for the clarification but this code also not working =) Try unserialize()
It's not small mistakes, they don't correctly searched for the end of the line, some randomly manner. - Theron.Waelchi64 commented on March 23rd 20 at 19:51
@Theron.Waelchi64, how does not work if I have displayed exactly the result that you wanted. If you handle exactly the string that you gave, it works. And if you have some scrap string and trying to process very different, it is certainly not the fact that will work. - greg49 commented on March 23rd 20 at 19:54
With unserialize() it's simple, thanks. - Theron.Waelchi64 commented on March 23rd 20 at 19:57
@greg49, and Yes I just had mb_substr, as said above use but mb_strpos, then work. - Theron.Waelchi64 commented on March 23rd 20 at 20:00
@greg49 Began to read your answer and it looks very strange code after the information about the unserialize. To the descendants not to be confused I'd swapped the last two paragraphs - it will be more consistent. - erik.Rogahn commented on March 23rd 20 at 20:03
@erik.Rogahn, Yes, it generally is better to remove what I did. This kind of code in General is not the place :) - greg49 commented on March 23rd 20 at 20:06
March 23rd 20 at 19:50
1. it's not JSON, and serialized string
2. perhaps due to multibyte encoding https://www.php.net/manual/ru/function.mb-substr.php
with mb_substr does not work, and in addition it's not JSON, don't know what to do =) How to get the value. - Theron.Waelchi64 commented on March 23rd 20 at 19:53
Although everything works with mb_strpos and mb_strpos. - Theron.Waelchi64 commented on March 23rd 20 at 19:56
March 23rd 20 at 19:52
The third argument is not the end position of the substring is the length of the required substring.
$namevalue=substr($findstr, $posnamevalstart + 1, $posnamevalend - $posnamevalstart - 1); // Loggia
March 23rd 20 at 19:54
The question that the answers - HELL.

How to write is PHP serialize, just not the whole line, and some stub
Therefore, it is necessary to take originalnuyu line, and to do so
$array = unserialize($findstr);
echo $array['name'];

Find more questions by tags PHP