php strpos特殊字符
I'm using PHP Version 5.1.6
I have a string (session file) and need to extract a value from it, so i'm searching for a needle in the string but it returns false, I reduced the code to this:
$string = ';SERVER_NAME|s:17:"stackoverflow.com";REMOTE_ADDR|s:13:"69.59.196.211";';
$start = strpos($string, ';SERVER_NAME|s:"');
echo $start; // prints nothing because $start = false
$start = strpos($string, 'SERVER_NAME|s:');
echo $start; // prints 1;
As you noticed if I have the character ';' or the character '"' in the needle, the search returns false, I tryed to use chr() for all characters in the needle but had the same result, If I remove the ';' and the '"' from the string if finds the needle in the string.
How can I search special characters in a string using PHP ?
我正在使用PHP版本5.1.6 p>
我有一个字符串 (会话文件)并需要从中提取一个值,所以我在字符串中搜索一个针但它返回false,我将代码缩减为: p>
$ string ='; SERVER_NAME | s:17:“stackoverflow.com”; REMOTE_ADDR | s:13:“69.59.196.211”;';
$ start = strpos($ string,'; SERVER_NAME | s:“') ;
echo $ start; //什么都不打印因为$ start = false
$ start = strpos($ string,'SERVER_NAME | s:');
echo $ start; //打印1;
code > pre>
正如您所注意到我的角色';' 或者针中的字符'“',搜索返回false,我试图对针中的所有字符使用chr()但结果相同,如果我删除';' 如果在字符串中找到了针,则来自字符串的'“'。 p>
如何使用PHP搜索字符串中的特殊字符? p>
div>
You are searching for ;SERVER_NAME|s:"
which clearly is not present in the haystack. I guess you meant to use ;SERVER_NAME|s:17:"
as the needle.
$start = strpos($string, ';SERVER_NAME|s:17:"');
echo $start; // prints 0
It was a typo that i missed, sorry problem solved. The correct code is:
$result = ';SERVER_NAME|s:17:"stackoverflow.com";REMOTE_ADDR|s:13:"69.59.196.211";';
$str_start = ';SERVER_NAME|s:';
$str_end = ':"';
$start = strpos($result, $str_start)+strlen($str_start);
$end = strpos($result, $str_end, $start);
$len = $end - $start;
$str_len = substr($result, $start, $len);
echo $server_name = substr($result, ($end + strlen($str_end)), $str_len);
and at the end it prints: stackoverflow.com
So it works, thank you all for help !
I'd say you think it is false because the string's position might be 0
which evaluates to FALSE
(assuming you are using a needle that is present in the string). You should always compare with ===
.
From the documentation (a big red warning box):
Warning
This function may return Boolean
FALSE
, but may also return a non-Boolean value which evaluates toFALSE
, such as0
or""
. Please read the section on Booleans for more information. Use the===
operator for testing the return value of this function.
It works perfectly for me with "
. The first echo prints nothing because the substring ;SERVER_NAME|s:"
is indeed not contained in the original string.
why not to unserialize
this string first?
as for these characters, there is nothing special in them
I had a problem with strpos as I started a needle string with one quote ' and searched for a string that contained double quote ".
Example 1 - Doesn't work:
strpos($text,'class="level')
Explanation: Double quote (") for some reason ends string even if we started string with one quote (').
Example 2 - Doesn't work:
strpos($text,'class=\"level')
Explanation: There is an escape sign before Double quote (") but it doesn't perform it's function.
Example 3 - Works!!:
strpos($text,"class=\"level")
Explanation: If you start needle string with double quote you can then escape double quote with backslash ().
Hope it helps....