tags:

views:

185

answers:

5

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 !

A: 

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 to FALSE, such as 0 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.

Felix Kling
the string position is not 0 in the code, just in the example I extracted just a part of the string. It was a typo.
Radu
@Radu: Ok. Anyway it is important to keep this in mind.
Felix Kling
A: 

why not to unserialize this string first?

as for these characters, there is nothing special in them

Col. Shrapnel
+2  A: 

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
codaddict
A: 

Gotcha: A little typo here:

$string =                 ';SERVER_NAME|s:17:"stackoverflow.com";REMOTE_ADDR|s:13:"69.59.196.211";';
$start  = strpos($string, ';SERVER_NAME|s:"');
// ---------------------------------------^
Salman A
A: 

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....

Tks, very helpfull information, you should log a BUG to PHP if there isn't one allready.
Radu