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 ?
EDIT:
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 !